str = 'foo 123 456 789 bar'
re = /^foo +(\d+) +(\d+) +(\d+) +bar$/
p re.match(str).to_a
#=> ["foo 123 456 789 bar", "123", "456", "789"]
_, a, b, c = re.match(str).to_a
p [a,b,c]
#=> ["123", "456", "789"]
Note that it Regex#matchwill return nilif no match is found, but nil.to_areturns an empty array, so the above is safe.
source
share