Answer from How do I get the match data for all occurrences of a Ruby regular expression in a string?:
input = "abc12def34ghijklmno567pqrs"
numbers = /\d+/
input.gsub(numbers) { |m| p $~ }
Result is as requested:
⇒ #<MatchData "12">
⇒ #<MatchData "34">
⇒ #<MatchData "567">
Would someone break down what the answerer is doing in input.gsub(numbers) { |m| p $~ }
?
Also, how would I access each of the MatchData
s?