Regular Expression to match cross platform newline characters

Question:

My program can accept data that has newline characters of n, rn 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.

Asked By: Alan

||

Answers:

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

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

Answered By: too much php

The pattern can be simplified to r?n for a little performance gain, as you probably don’t have to deal with the old Mac style (OS 9 is unsupported since February 2002).

Answered By: Diego V
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.