1

Is there a way to match a line break independently of system? i.e. match both \n and \r\n. The only thing I can think of is \r?\n which just feels clunky.

The reason I want to do this is

  1. if I need to match 2 in a row, \n\n no longer works and
  2. if I match \n, then the preceding \r will still exist and I would have to strip it off or it could lead to problems later
ewok
  • 20,148
  • 51
  • 149
  • 254

2 Answers2

4

Wouldn't this get all groups of adjacent \r and \n characters regardless of order or amount?

Edited per comments:

[\r\n]+

GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19
3

This should be good for cross-platform line break

regex = '(\r\n?|\n)+'