解析 ruby 字符串中的重复匹配项?
也许我只是很累了,但我没有看到一种明显的方法来解析这样的字符串中的数字:
some text here, and then 725.010, 725.045, 725.340 and 725.370; and more text
我想到的一件事:将其按空格分割成一个数组。然后对数组中的每个元素应用一个组的正则表达式测试。
有没有更干净、更简单的方法来做到这一点?
Maybe I'm just pretty tired, but I don't see an obvious way to parse out the numbers from a string like this:
some text here, and then 725.010, 725.045, 725.340 and 725.370; and more text
One thing that occurred to me: split this by spaces into an array. Then apply a regex test with a group to each element in the array.
Is there a cleaner, simpler way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要 String#scan:
将输出匹配数组。
You need String#scan:
will output array of matches.
这将在字符串中查找整数或小数,并将它们(但仍然作为字符串)放入数组
result
中。This will find integers or decimals in a string and put them (still as strings, though) into the array
result
.