As basically stated in the question title, is there a method on Ruby strings that is the equivalent to String#Scan but instead of returning just a list of each match, it would r
If you don't need to get MatchData
s back, here's a way using StringScanner
.
require 'strscan'
rxp = /_[^_]+_/
scanner = StringScanner.new "foo _barrrr_ _baz_ hashbang"
match_infos = []
until scanner.eos?
scanner.scan_until rxp
if scanner.matched?
match_infos << {
pos: scanner.pre_match.size,
length: scanner.matched_size,
match: scanner.matched
}
else
break
end
end
p match_infos
# [{:pos=>4, :length=>8, :match=>"_barrrr_"}, {:pos=>13, :length=>5, :match=>"_baz_"}]