Regular Expression to match cross platform newline characters

不打扰是莪最后的温柔 提交于 2019-11-26 12:58:27

问题


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 will match whatever the encoding is?

Alternatively, I could use universal_newline support on input, but now I\'m interested to see what the regex would be.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1331815/regular-expression-to-match-cross-platform-newline-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!