Find 2 Or More Newlines
My string looks like: 'I saw a little hermit crab\r\nHis coloring was oh so drab\r\n\r\nIt\u2019s hard to see the butterfly\r\nBecause he flies across the sky\r\n\r\nHear the honki
Solution 1:
re.split(r'(\r\n){2,}', text)
doesn't split at every line. It does exactly what you want, except it preserves one occurence of \r\n
because you've enclosed it in a capturing group. Use a non-capturing group instead:
(?:\r\n){2,}
Here you can see what the difference is:
>>> re.split(r'(?:\r\n){2,}', 'foo\r\n\r\nbar')
['foo', 'bar']
>>> re.split(r'(\r\n){2,}', 'foo\r\n\r\nbar')
['foo', '\r\n', 'bar']
Solution 2:
You want to use a Non-capturing group instead of a capturing group when you execute the call to re.split()
. In the documentation, it is clearly stated that using a capturing group retains the separator pattern:
If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.
re.split(r'(?:\r\n){2,}', text)
Post a Comment for "Find 2 Or More Newlines"