Certainly splitlines() is the right tool for the job.
The following solutions may help if all you need is to deal with CR, \r (carriage return) and LF, \n (line feed character):
re.findall('[^\r\n]+', given_text) # Returns all non-empty lines split with one or more CR/LF chars
re.split(r'\r\n?|\n', given_text) # Splits with the most common CRLF, CR or LF line endings
Note the re.split solution will return empty lines, too.
Details
[^\r\n]+ - one or more chars other than CR and LF chars
\r\n?|\n - a CR and an optional LF char (\r\n?) or (|) a newline, LF, only (\n)
If you need to support all possible Unicode line breaks, you can use
re.findall(r'[^\r\n\x0B\x0C\x85\u2028\u2029]+', given_text)
re.split(r'\r\n?|[\n\x0B\x0C\x85\u2028\u2029]', given_text)
NOTES:
| Char |
Description |
\r (\x0D) |
CARRIAGE RETURN, CR |
\n (\x0A) |
LINE FEED, LF |
\x0B |
LINE TABULATION, LT |
\x0C |
FORM FEED, FF |
\x85 |
NEXT LINE, NEL |
\u2028 |
LINE SEPARATOR, LS |
\u2029 |
PARAGRAPH SEPARATOR, PS |
See a Python demo:
import re
given_text = '1stline\n2ndline\r3rdline\r\n4thline\r\n\r\nLast Line after an empty line'
print( re.findall('[^\r\n]+', given_text) )
# => ['1stline', '2ndline', '3rdline', '4thline', 'Last Line after an empty line']
print( re.split(r'\r\n?|\n', given_text) )
# => ['1stline', '2ndline', '3rdline', '4thline', '', 'Last Line after an empty line']
print( re.findall(r'[^\r\n\x0B\x0C\x85\u2028\u2029]+', given_text) )
# => ['1stline', '2ndline', '3rdline', '4thline', 'Last Line after an empty line']
print( re.split(r'\r\n?|[\n\x0B\x0C\x85\u2028\u2029]', given_text) )
# => ['1stline', '2ndline', '3rdline', '4thline', '', 'Last Line after an empty line']
re.findall('[^\r\n]+', given_text). Or, you may usere.split(r'\r\n?|\n', given_text)if you need to get empty lines, too.