Alex Rivera | Logout

Regex to split on successions of newline characters

Asked 2010-04-08T00:20:56.480
11

I'm trying to split a string on newline characters (catering for Windows, OS X, and Unix text file newline characters). If there are any succession of these, I want to split on that too and not include any in the result.

So, for when splitting the following:

"Foo\r\n\r\nDouble Windows\r\rDouble OS X\n\nDouble Unix\r\nWindows\rOS X\nUnix"

The result would be:

['Foo', 'Double Windows', 'Double OS X', 'Double Unix', 'Windows', 'OS X', 'Unix']

What regex should I use?

Edit
Report

1 Answer

2
>>> s="Foo\r\n\r\nDouble Windows\r\rDouble OS X\n\nDouble Unix\r\nWindows\rOS X\nUnix"
>>> import re
>>> re.split("[\r\n]+",s)
['Foo', 'Double Windows', 'Double OS X', 'Double Unix', 'Windows', 'OS X', 'Unix']
answered 2010-04-08T00:34:50.553

Your Answer