regex – Match linebreaks – n or rn?

regex – Match linebreaks – n or rn?

Gonna answer in opposite direction.

2) For a full explanation about r and n I have to refer to this question, which is far more complete than I will post here: Difference between n and r?

Long story short, Linux uses n for a new-line, Windows rn and old Macs r. So there are multiple ways to write a newline. Your second tool (RegExr) does for example match on the single r.

1) [rn]+ as Ilya suggested will work, but will also match multiple consecutive new-lines. (rn|r|n) is more correct.

In PCRE R matches n, r and rn.

regex – Match linebreaks – n or rn?

You have different line endings in the example texts in Debuggex. What is especially interesting is that Debuggex seems to have identified which line ending style you used first, and it converts all additional line endings entered to that style.

I used Notepad++ to paste sample text in Unix and Windows format into Debuggex, and whichever I pasted first is what that session of Debuggex stuck with.

So, you should wash your text through your text editor before pasting it into Debuggex. Ensure that youre pasting the style you want. Debuggex defaults to Unix style (n).

Also, NEL (u0085) is something different entirely: https://en.wikipedia.org/wiki/Newline#Unicode

(r?n) will cover Unix and Windows. Youll need something more complex, like (rn|r|n), if you want to match old Mac too.

Leave a Reply

Your email address will not be published.