Python regex — extraneous matchings

后端 未结 5 1241
执念已碎
执念已碎 2021-01-18 07:17

I want to split a string using -, +=, ==, =, +, and white-space as delimiters. I want to keep the delimiter

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-18 08:13

    Why is it behaving this way?

    According to the documentation for re.split:

    If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.

    This is literally correct: if capturing parentheses are used, then the text of all groups are returned, whether or not they matched anything; the ones which didn't match anything return None.

    As always with split, two consecutive delimiters are considered to separate empty strings, so you get empty strings interspersed.

    how might I change it to get what I want?

    The simplest solution is to filter the output:

    filter(None, pattern.split(s))
    

提交回复
热议问题