简体   繁体   中英

Regular Expression to match cross platform newline characters

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.

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.

该模式可以简化为\\r?\\n以获得一点性能提升,因为您可能不需要处理旧的Mac风格(自2002年2月以来OS 9不受支持)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM