开发者

Parsing repeated matches from a ruby string?

开发者 https://www.devze.com 2023-01-29 20:23 出处:网络
Maybe I\'m just pretty tired, but I don\'t see an obvious way to parse out the numbers from a string like this:

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?


You need String#scan:

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

will output array of matches.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号