From: Alex Young Date: 2007-01-26T23:31:11+09:00 Subject: Re: simple question about Ruby Regext Peter Bailey wrote: > Alex Young wrote: >> Peter Bailey wrote: >>> This is driving me nuts. Why isn't the RUBY response "=>2?" There are >>> two "an" stubs in "banana." >> The number returned is the position of the start of match, not the >> number of them. >> >>> I thought that RUBY, like PERL, is inherently greedy and it would find >>> all instances of said regex expression. >> It is... there's only one match, and it matches everything from the >> first 'a' to the end of the string. >> >> Hope this makes sense, > > Thanks, Gentlemen. I'll watch my spelling of Ruby and Perl from now on. > > I understand now that my match is only looking for the position. Cool. > Thanks. But, . . ., here's a similar regex where I don't want the > position, but I want to change all instances of the stub, and, it's only > changing the first one. > > "banana".sub(/an/, "ze") > => "bzeana" That's explicitly what sub is for - only changing the first one. You want gsub. -- Alex