OscarRyz OscarRyz - 1 year ago 76
Ruby Question

Extract number from string in Ruby

I'm using this code:

s = line.match( /ABCD(\d{4})/ ).values_at( 1 )[0]


To extract numbers from strings like:

ABCD1234
ABCD1235
ABCD1236


etc.

It works, but I wonder what other alternative I have to to this in Ruby?

My code:

ids = []
someBigString.lines.each {|line|
ids << line.match( /ABCD(\d{4})/ ).values_at( 1 )[0]
}

Answer Source
a.map {|x| x[/\d+/]}