From: Xeno Campanoli Date: 2006-04-27T14:58:48+09:00 Subject: Re: Stumped: How do I iterate over regular expression matches? --------------040600000604090006020603 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Thank you all for the answers. I should have guessed to look at the String methods. Sincerely, Xeno xc John Johnson wrote: >You can use String#scan: > >irb >irb(main):001:0> a="This is a test of this thing for this guy on the net." >=> "This is a test of this thing for this guy on the net." >irb(main):002:0> a.scan(/is/) >=> ["is", "is", "is", "is"] >irb(main):003:0> a.scan(/[^ ]is/) >=> ["his", "his", "his"] >irb(main):004:0> a.scan(/[^ ]+is/) >=> ["This", "this", "this"] >irb(main):005:0> > >or: > >a.scan(/[^ ]+is/) { |match| > puts "#{match} is a match of the regexp." >} >This is a match of the regexp. >this is a match of the regexp. >this is a match of the regexp. > >Regards, > JJ > > > >On Wednesday, April 26, 2006, at 02:21PM, Xeno Campanoli wrote: > > > >>I looked for an iterator, and there may be something, but I don't recognize it. >>I also tried: >>while (x =~ /something/ ) >>p $1 >> >>end >>and it just printed out the same match over and over. Perl does this very >>nicely, but google and pickaxe are not helping me do it in Ruby. Sorry if this >>is obvious. >> >>xc >> >> >> >> >> > > >--- >Help everyone. If you can't do that, then at least be nice. > > > > > > -- Xeno Campanoli: http://www.eskimo.com/~xeno "Even the President of the United States sometimes must have to stand naked." - Bob Dylan --------------040600000604090006020603--