解析 ruby​​ 字符串中的重复匹配项?

发布于 2024-10-07 18:18:09 字数 231 浏览 0 评论 0原文

也许我只是很累了,但我没有看到一种明显的方法来解析这样的字符串中的数字:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱&纷飞 2024-10-14 18:18:09

您需要 String#scan

your_string.scan(/\d\d\d\.\d\d\d/)

将输出匹配数组。

You need String#scan:

your_string.scan(/\d\d\d\.\d\d\d/)

will output array of matches.

许仙没带伞 2024-10-14 18:18:09
result = subject.scan(/\d+(?:\.\d+)?/)

这将在字符串中查找整数或小数,并将它们(但仍然作为字符串)放入数组 result 中。

result = subject.scan(/\d+(?:\.\d+)?/)

This will find integers or decimals in a string and put them (still as strings, though) into the array result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文