[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

simple question about Ruby Regext

Peter Bailey

1/26/2007 1:58:00 PM

Hi,
I've been learning RUBY the past 7 months or so, and, now, my assistant
is doing the same. In her perusal of the "Programming RUBY" book, first
edition, she's come across a simple, simple regex truism that throws
her, and throws me, too!

Why is this true?

"banana" =~ /an*/
=>1

This is driving me nuts. Why isn't the RUBY response "=>2?" There are
two "an" stubs in "banana."

I thought that RUBY, like PERL, is inherently greedy and it would find
all instances of said regex expression.

Thanks a lot!
Peter

--
Posted via http://www.ruby-....

20 Answers

Alex Young

1/26/2007 2:01:00 PM

0

Peter Bailey wrote:
> Hi,
> I've been learning RUBY the past 7 months or so, and, now, my assistant
> is doing the same. In her perusal of the "Programming RUBY" book, first
> edition, she's come across a simple, simple regex truism that throws
> her, and throws me, too!
>
> Why is this true?
>
> "banana" =~ /an*/
> =>1
>
> 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,
--
Alex

James Gray

1/26/2007 2:10:00 PM

0

On Jan 26, 2007, at 7:57 AM, Peter Bailey wrote:

> I thought that RUBY, like PERL, is inherently greedy and it would find
> all instances of said regex expression.

I see you already have your answer, so I will just add that those
languages are spelled Ruby and Perl. Welcome to Ruby!

James Edward Gray II

Peter Bailey

1/26/2007 2:25:00 PM

0

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"

What's with that?

-Peter

--
Posted via http://www.ruby-....

Robert Klemme

1/26/2007 2:29:00 PM

0

On 26.01.2007 15:00, Alex Young wrote:
> Peter Bailey wrote:
>> Hi,
>> I've been learning RUBY the past 7 months or so, and, now, my assistant
>> is doing the same. In her perusal of the "Programming RUBY" book, first
>> edition, she's come across a simple, simple regex truism that throws
>> her, and throws me, too!
>>
>> Why is this true?
>>
>> "banana" =~ /an*/
>> =>1
>>
>> 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.

No. It's just matching "an":

irb(main):001:0> "banana"[/an*/]
=> "an"

You were right if the regexp had a dot:

irb(main):002:0> "banana"[/an.*/]
=> "anana"

robert

Robert Klemme

1/26/2007 2:30:00 PM

0

On 26.01.2007 15:24, 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"
>
> What's with that?

ri String#sub
ri String#gsub

And, for completeness reasons

ri String#sub!
ri String#gsub!

Kind regards

robert

Alex Young

1/26/2007 2:31:00 PM

0

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

Alex Young

1/26/2007 2:32:00 PM

0

Robert Klemme wrote:
> On 26.01.2007 15:00, Alex Young wrote:
>> Peter Bailey wrote:
>>> Hi,
>>> I've been learning RUBY the past 7 months or so, and, now, my assistant
>>> is doing the same. In her perusal of the "Programming RUBY" book, first
>>> edition, she's come across a simple, simple regex truism that throws
>>> her, and throws me, too!
>>>
>>> Why is this true?
>>>
>>> "banana" =~ /an*/
>>> =>1
>>>
>>> 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.
>
> No. It's just matching "an":
>
> irb(main):001:0> "banana"[/an*/]
> => "an"
>
> You were right if the regexp had a dot:
>
> irb(main):002:0> "banana"[/an.*/]
> => "anana"
>
Oops :-) Sorry for any confusion. Not enough blood in my caffeine
system, obviously :-)

--
Alex

WoNáDo

1/26/2007 2:34:00 PM

0

Peter Bailey schrieb:
> Why is this true?
>
> "banana" =~ /an*/
> =>1
As already said, it is the position of the match.

> I thought that RUBY, like PERL, is inherently greedy and it would find
> all instances of said regex expression.

If you want to find all matches, you should use "scan" in Ruby. There are two
possibilities. the first one...

str = 'banana'
str.scan(/an/) # => ["an", "an"]

....will produce an Array object for each match (see "ri String#scan" for
details). The second one will invoke a block for each match.

str = 'banana'
str.scan(/an/) do
puts "#$`<#$&>#$'"
end

produces:

b<an>ana
ban<an>a

Wolfgang Nádasi-Donner

Peter Bailey

1/26/2007 2:37:00 PM

0

Alex Young wrote:
> Peter Bailey wrote:
>>> first 'a' to the end of the string.
>> "banana".sub(/an/, "ze")
>> => "bzeana"
> That's explicitly what sub is for - only changing the first one. You
> want gsub.

Got it. Thanks, guys. Yes, I should've used gsub, of course. So, I'm not
going nuts.

--
Posted via http://www.ruby-....

Peter Bailey

1/26/2007 2:45:00 PM

0


Thanks, Wolfgang. So, you suggest the use of "scan" instead of "gsub?"
That would imply the need for a block, which, seems kind of wordy, but,
it does have power. . . . Thanks again.

--
Posted via http://www.ruby-....