I need the MatchData for each occurrence of a regular expression in a string. This is different than the scan method suggested in Match All Occurrences of a Reg
MatchData
I’ll put it here to make the code available via a search:
input = "abc12def34ghijklmno567pqrs" numbers = /\d+/ input.gsub(numbers) { |m| p $~ }
The result is as requested:
⇒ # ⇒ # ⇒ #
See "input.gsub(numbers) { |m| p $~ } Matching data in Ruby for all occurrences in a string" for more information.