From: "David A. Black" Date: 2008-06-12T20:29:38+09:00 Subject: Re: Newby Ruby Regular Expression Question Hi -- On Thu, 12 Jun 2008, Ruchira Bomiriya wrote: > Dear Expert, > > I have recently started Ruby programming. I have used Perl before and > liked it mostly due to the power of regular expressions. But Ruby's > ability scale and modularize is absolutely amazing. > > I want to find FOUR instances of A in the string "AaAbAcAd" using > regular expressions. > > I tried > > str = "AaAbAcAd" > md = str.match(/(A)/) > > and get just one instance (seems like the last 'A') > > The perl equivalent of what I really want is > > $str = "AaAbAcAd" > @md = ($str =~ /A/g) > > So what I really want to find out is if there is something equivalent > to the Perl g option available in Ruby. How would you get all FOUR > matches in a reasonable way? str.scan(/A/). I can't help wondering though... what made you think that the A you got is the last A? (It's actually the first A.) David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin ADVANCING WITH RAILS July 21-24 Edison, NJ See http://www.rubypal.com for details and updates!