views:

73

answers:

3

I am sure this has been asked before, but I cannot find it.

Basically, assuming you are parsing a text file of unknown origin and want to replace line breaks with some other delimiter, is this the best regex, or is there another?

(\r\n)|(\n)|(\r)

A: 

Just replace /[\r\n]+/g with an empty string "".

It'll replace all \r and \n no matter what order they appear in the string.

Amarghosh
This will replace any number of line breaks with one replacement token.
Andreas Jansson
@Andreas He wants to remove line breaks.
Amarghosh
Well, he wants to replace them with another delimiter...
Tim Pietzcker
+2  A: 

Feltcher - this did get asked once before.

Here you go: http://stackoverflow.com/questions/1331815/regular-expression-to-match-cross-platform-newline-characters

  • Spoiler Alert!

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

Bobby B
+2  A: 

Do check if your regex engine supports \R as a shorthand character class and you will not need to be concerned with the various Unicode newline / linefeed combos. If implemented correctly, you can then match all the various ascii or Unicode line endings transparently using \R.

In Unicode you need to detect NEL (OS/390 line ending, \x85) LS (Line Separator, \x2028) and PS (Paragraph Separator, \x2029) if you want to be completely cross platform these days.

It is debatable whether LS, NEL, and PS should be treated as line breaks, line endings, or white space. The XML 1.0 standard, for example, does not recognize NEL as a line break character. ECMAScript treats LS and PS as line breaks but NEL as whitespace. Perl unicode regexs will treat VT, FF, CR, CRLF, NEL, LS and PS as line breaks for the purpose of ^ and $ regex meta characters.

The Unicode Implementation Guide (section 5.8 and table 5.3) is probably the best bet of what the definitive treatment of what a "newline" is.

If you are only concerned with ascii with the DOS/Windows/Unix/Mac classic variants, the regex equivalent to \R is (?>\r\n|[\r\n])

In Unicode, the equivalent to \R is (?>\r\n|\n|\x0b|\f|\r|\x85|\x2028|\x2029) The \x0b in there is a vertical tab; once again, this may or may not fit you definition of what a line break is, but that does match the recommendation of the Unicode Implantation. (FF, or \x0C is not included in the regex since a Form Feed is a new page, not a new line in the definition.)

drewk
"utf8" in your answer should be "Unicode". UTF-8 is merely one of the Unicode character encodings.
Alan Moore
You are right, but the docs I was referring to (PCRE manual) had the same issue! Edit made...
drewk