拆分为不同的换行符
现在我正在对字符串进行 split
并假设用户的换行符是 \r\n
,如下所示:
string.split(/\r\n/)
我想做的是拆分位于 \r\n
或仅 \n
上。
那么正则表达式将如何分割其中的任何一个呢?
Right now I'm doing a split
on a string and assuming that the newline from the user is \r\n
like so:
string.split(/\r\n/)
What I'd like to do is split on either \r\n
or just \n
.
So how what would the regex be to split on either of those?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您尝试过
/\r?\n/
吗??
使\r
成为可选。用法示例: http://rubular.com/r/1ZuihD0YfF
Did you try
/\r?\n/
? The?
makes the\r
optional.Example usage: http://rubular.com/r/1ZuihD0YfF
Ruby 有方法
String#each_line
和String#lines
返回一个枚举:
http://www.ruby-doc.org/core-1.9。 3/String.html#method-i-each_line
返回一个数组:
http://www.ruby-doc.org/core-2.1。 2/String.html#method-i-lines
我没有针对您的场景进行测试,但我敢打赌它会比手动选择换行符更好。
Ruby has the methods
String#each_line
andString#lines
returns an enum:
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-each_line
returns an array:
http://www.ruby-doc.org/core-2.1.2/String.html#method-i-lines
I didn't test it against your scenario but I bet it will work better than manually choosing the newline chars.
尽管它对解决这个问题没有帮助(您确实需要正则表达式),但请注意
String#split
不需要正则表达式参数。您的原始代码也可能是string.split( "\r\n" )
。Although it doesn't help with this question (where you do need a regex), note that
String#split
does not require a regex argument. Your original code could also have beenstring.split( "\r\n" )
.为了操作系统的安全。我会这样做 /\r?\n|\r\n?/
To be safe for operating systems. I would do /\r?\n|\r\n?/
Ruby
Regexp
中的交替运算符与标准正则表达式中的相同:|
因此,显而易见的解决方案
相同
是与可选的
\r
后跟强制\n
。The alternation operator in Ruby
Regexp
is the same as in standard regular expressions:|
So, the obvious solution would be
which is the same as
i.e. an optional
\r
followed by a mandatory\n
.您是从文件读取还是从标准输入读取?如果您正在从文件读取,并且该文件处于文本模式而不是二进制模式,或者您正在从标准输入读取,您不必处理
\r\n
- 它看起来就像\n
一样。Are you reading from a file, or from standard in?If you're reading from a file, and the file is in text mode, rather than binary mode, or you're reading from standard in, you won't have to deal with
\r\n
- it'll just look like\n
.也许只对“\n”进行拆分并删除“\r”(如果存在)?
Perhaps do a split on only '\n' and remove the '\r' if it exists?
另一种选择是使用 String#chomp,它还可以自行智能地处理换行符。
您可以通过以下方式完成您所追求的目标:
或者,如果您正在处理足够大的问题而需要考虑内存使用问题:
在解决此类问题时,性能并不总是最重要的,但值得注意的是解决方案也比使用正则表达式快一点。
在我的机器上(i7,ruby 2.1.9):
Another option is to use String#chomp, which also handles newlines intelligently by itself.
You can accomplish what you are after with something like:
Or if you are dealing with something large enough that memory use is a concern:
Performance isn't always the most important thing when solving this kind of problem, but it is worth noting the chomp solution is also a bit faster than using a regex.
On my machine (i7, ruby 2.1.9):