Regular Expression to match cross platform newline characters

前端 未结 2 647
鱼传尺愫
鱼传尺愫 2020-12-01 03:41

My program can accept data that has newline characters of \\n, \\r\\n or \\r (eg Unix, PC or Mac styles)

What is the best way to construct a regular expression that

相关标签:
2条回答
  • 2020-12-01 03:55

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

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

    0 讨论(0)
  • 2020-12-01 04:01

    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).

    0 讨论(0)
提交回复
热议问题