OscarRyz OscarRyz - 4 months ago 22
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
a.map {|x| x[/\d+/]}
Comments