From: 7stud -- Date: 2011-04-06T10:37:16+09:00 Subject: Re: Match a pattern multiple times, returning matches, captures and offset? Markus Fischer wrote in post #991092: > > But here I'm missing the further possible matches, "_bar_" and "bar". Or > the #scan approach: > > ruby-1.9.2-p180 :004 > m = "_foo_ _bar_".scan(/_(\w+)_/) > => [["foo"], ["bar"]] > > But in this case I've even less information, the match including _foo_ > or _bar_ is not present and I can't get the offsets too. > > I re-read the documentation for Regexp#match If you look at the preamble in the docs for the MatchData class, you can retrieve a MatchData object using Regexp.last_match, which you can call inside a scan() block: str = "_foo_ _bar_" str.scan(/_(\w+)_/) do |match| md = Regexp.last_match p [md[0], md[1], md.offset(1)] end --output:-- ["_foo_", "foo", [1, 4]] ["_bar_", "bar", [7, 10]] -- Posted via http://www.ruby-forum.com/.