Alex Young
1/26/2007 2:32:00 PM
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