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

前端 未结 4 1895
别那么骄傲
别那么骄傲 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

    Basically, split() is two different functions into one. If you provide a parameter, it behaves very differently than when called without one.

    At first, it would seems that

    s.split() == s.split(' \t\n')
    

    but this is not the case, as you have shown. The doc says:

    [...] If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. [...]

    Even adding a 'remove_empty' parameter it would still behave weird, because the default of 'remove_empty' depends on the 'sep' parameter being there.

提交回复
热议问题