Regexp to extra a all numbers

B

Bill Grundler

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.
 
T

Tim Hunter

Bill said:
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.

You're very close. Try this

"923 a 222".scan(/\d+/)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,368
Members
48,060
Latest member
JerrodSimc

Latest Threads

Top