Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail From: Brian Candler Newsgroups: comp.lang.ruby Subject: Re: Match a pattern multiple times, returning matches, captures and offset? Date: Tue, 5 Apr 2011 13:07:41 -0500 Organization: Service de news de lacave.net Lines: 15 Message-ID: <61d24bb96a28e517e89adef15f444b29@ruby-forum.com> References: <4D9B4FBD.9020602@fischer.name> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1302028573 65483 65.111.164.187 (5 Apr 2011 18:36:13 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Tue, 5 Apr 2011 18:36:13 +0000 (UTC) In-Reply-To: <4D9B4FBD.9020602@fischer.name> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381021 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <61d24bb96a28e517e89adef15f444b29@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2362 String#scan with a block may do what you want: >> "_foo_ _bar_".scan(/_(\w+)_/) { |x| puts "Offset #{$`.size}, captures #{x.inspect}" } Offset 0, captures ["foo"] Offset 6, captures ["bar"] => "_foo_ _bar_" But it doesn't give you offsets to the individual captures, just to the start of the whole match. (You also get the full match in $& and the rest of the string after the match in $') -- Posted via http://www.ruby-forum.com/.