Why doesn't Python's `re.split()` split on zero-length matches?

前端 未结 4 1891
别那么骄傲
别那么骄傲 2020-12-01 18:39

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

4条回答
  •  半阙折子戏
    2020-12-01 19:03

    To workaround this problem, you can use the VERSION1 mode of the regex package which makes split() produce zero-length matches as well:

    >>> import regex as re
    >>> re.split(r"\s+|\b", "Split along words, preserve punctuation!", flags=re.V1)
    ['', 'Split', 'along', 'words', ',', 'preserve', 'punctuation', '!']
    

提交回复
热议问题