One particular quirk of the (otherwise quite powerful)
>>> re.split(r"\s+|\b", "Split along words, preserve punctuation!")
['Split', 'along', 'words,', 'preserve', 'punctuation!']
['', 'Split', 'along', 'words', ',', 'preserve', 'punctuation', '!']
It's a design decision that was made, and could have gone either way. Tim Peters made this post to explain:
For example, if you split "abc" by the pattern x*, what do you expect? The pattern matches (with length 0) at 4 places, but I bet most people would be surprised to get
['', 'a', 'b', 'c', '']
back instead of (as they do get)
I'm okay with adding a flag to enable this behavior though.
There is a workaround posted by Jan Burgy:
>>> s = "Split along words, preserve punctuation!" >>> re.sub(r"\s+|\b", '\f', s).split('\f') ['', 'Split', 'along', 'words', ',', 'preserve', 'punctuation', '!']
'\f' can be replaced by any unused character.