Alan Alan - 1 year ago 102
Python Question

Regular Expression to match cross platform newline characters

My program can accept data that has newline characters of \n, \r\n or \r (eg Unix, PC or Mac styles)

What is the best way to construct a regular expression that will match whatever the encoding is?

Alternatively, I could use universal_newline support on input, but now I'm interested to see what the regex would be.

Answer Source

The regex I use when I want to be precise is "\r\n?|\n".

When I'm not concerned about consistency or empty lines, I use "[\r\n]+", I imagine it makes my programs somewhere in the order of 0.2% faster.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download