Regexp to extra a all numbers

I’m trying to get an array of every number in a string that is grouped
together. This is the code I thought might do it, but does not:

irb(main):064:0> “923 a 222”.match(/\d+/).to_a
=> [“923”]

I’m sure there’s a simple way to do this, but I’m not that well versed
with Regular Expressions.

The results I was expected (and the results that are desired) are:

[“923”, “222”]

Thanks in advance for any help.

Bill G. wrote:

[“923”, “222”]

Thanks in advance for any help.

You’re very close. Try this

“923 a 222”.scan(/\d+/)

Excellent. Thanks so much Tim.

Tim H. wrote:

Bill G. wrote:

[“923”, “222”]

Thanks in advance for any help.

You’re very close. Try this

“923 a 222”.scan(/\d+/)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs