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
  • 18,668
  • 45
  • 132
  • 245

2 Answers2

2

This should be good for cross-platform line break

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

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

Edited per comments:

[\r\n]+

GantTheWanderer
  • 1,226
  • 1
  • 10
  • 19