11 ms·
I find it difficult to be interested in this philosophical difference between the two when only difference is in how they treat the empty string. That is, spli
by eesmith 3y ago
I find it difficult to be interested in this philosophical difference between the two when only difference is in how they treat the empty string.
That is, splitting with regex look-behind/look-ahead, like:
(?<=\n)(?=.)
with a suitable definition of dot, also matches "negative space", and produces the same results as Python's str.splitlines(True), except for the empty string.
>>> import re
>>> p = re.compile(r"(?<=\n)(?=.)", re.DOTALL)
>>> def check(s):
... lines = p.split(s)
... assert lines == s.splitlines(True), lines
... return lines
...
>>> check("no newline at end of file")
['no newline at end of file']
>>> check("\nno-newline")
['\n', 'no-newline']
>>> check("line\nno-newline")
['line\n', 'no-newline']
>>> check("line\nline\n")
['line\n', 'line\n']
>>> check("\n")
['\n']
>>> check("\n\n")
['\n', '\n']
>>> check("\n\n\n")
['\n', '\n', '\n']
>>> check("")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in check
AssertionError: ['']