I want to split a string using -
, +=
, ==
, =
, +
, and white-space as delimiters. I want to keep the delimiter
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))