正则表达式以匹配跨平台换行符 [英] Regular Expression to match cross platform newline characters

查看:97
本文介绍了正则表达式以匹配跨平台换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序可以接受具有\n,\r\n或\r换行符的数据(例如Unix,PC或Mac样式)

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?

或者,我可以在输入中使用Universal_newline支持,但是现在我很感兴趣

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

推荐答案

我想精确使用的正则表达式是 \r\n?| \n

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

当我不关心一致性或空行时,我使用 [\r\n] + ,我想它会使我的程序快0.2%。

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.

这篇关于正则表达式以匹配跨平台换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆