From: Brian Candler Date: 2011-04-07T17:39:17+09:00 Subject: Re: Match a pattern multiple times, returning matches, captures and offset? 7stud -- wrote in post #991338: > You can also get relative beginning offsets like this: > > str = "_foo_ _bar_" > > str.scan(/_(\w+)_/) do |curr_match| > md = Regexp.last_match > whole_match = md[0] > captures = md.captures > > captures.each do |capture| > p [whole_match, capture, whole_match.index(capture)] > end > > end Using 'index' doesn't work if you have multiple captures which have the same pattern, or one is a substring of the other. Use captures.begin and captures.end instead. >> md = /(...)(...)/.match "foofoo" => # >> md.captures => ["foo", "foo"] >> md.begin(1) => 0 >> md.begin(2) => 3 -- Posted via http://www.ruby-forum.com/.