One particular quirk of the (otherwise quite powerful) re module in Python is that re.split() will never split a string on a zero-length match, for example if I want to split a string along word boundaries:
>>> re.split(r"\s+|\b", "Split along words, preserve punctuation!")
['Split', 'along', 'words,', 'preserve', 'punctuation!']
instead of
['', 'Split', 'along', 'words', ',', 'preserve', 'punctuation', '!']
Why does it have this limitation? Is it by design? Do other regex flavors behave like this?
It’s a design decision that was made, and could have gone either way. Tim Peters made this post to explain:
Some others disagree with him though. Guido van Rossum doesn’t want it changed due to backwards compatibility issues. He did say:
Edit:
There is a workaround posted by Jan Burgy:
Where
'\f'can be replaced by any unused character.