开发者

Parsing repeated matches from a ruby string?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜