如何在 Ruby 中解析制表符分隔的文本行?
我发现 Ruby 的 each
函数有点令人困惑。 如果我有一行文本,each
循环将为我提供每个空格分隔的单词,而不是每个单独的字符。
那么检索由制表符分隔的字符串部分的最佳方法是什么。 目前我有:
line.split.each do |word|
...
end
但这并不完全正确。
I find Ruby's each
function a bit confusing. If I have a line of text, an each
loop will give me every space-delimited word rather than each individual character.
So what's the best way of retrieving sections of the string which are delimited by a tab character. At the moment I have:
line.split.each do |word|
...
end
but that is not quite correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我完全理解你的问题,但是如果你想在制表符上分割行,你可以将其指定为 split: 的参数,
或者你可以将其指定为正则表达式:
每个基本上只是迭代所有数组中的项目, split 从字符串生成数组。
I'm not sure I quite understand your question, but if you want to split the lines on tab characters, you can specify that as an argument to split:
or you can specify it as a regular expression:
Each basically just iterates through all the items in an array, and split produces an array from a string.