Dogweather
Dogweather

Reputation: 16819

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?

Upvotes: 4

Views: 1514

Answers (2)

Nakilon
Nakilon

Reputation: 35112

You need String#scan:

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

will output array of matches.

Upvotes: 5

Tim Pietzcker
Tim Pietzcker

Reputation: 336478

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.

Upvotes: 5

Related Questions