Parsing duplicate matches from a ruby ​​string?

Maybe I'm just tired, but I don't see an obvious way to parse 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 crossed my mind: divide it with spaces into an array. Then apply a regular expression test with a group to each element of the array.

Is there a simpler and easier way to do this?

+4
source share
2 answers

You need String # scan :

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

will output an array of matches.

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

This will find integers or decimal numbers in the string and put them (yet as strings) in the result array.

+5
source

All Articles