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.
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.
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.
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.
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.
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.
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 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.
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
Which one you use depends mainly on your purpose. Here's how I view the methods, in terms of a "Find and Replace" feature set common to so many programs:
sub/sub! == Find
gsub/gsub! == Find and Replace
scan == Find All
Hope that helps.
James Edward Gray II
···
On Jan 26, 2007, at 8:45 AM, Peter Bailey wrote:
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.
This is indeed the most generous of forums. I would've been flamed long
ago in the Perl world.
I suggest that it's not a feature of Perl per se, but of the larger number of people in Perl. Given a large enough group, you'll always have a percentage of people who are jerks. I've already run into people who've treated me poorly because I was new and unfamiliar to them.
xoxo,
Andy
···
--
Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance
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.
gsub/gsub! also take a block.
Which one you use depends mainly on your purpose. Here's how I view
the methods, in terms of a "Find and Replace" feature set common to
so many programs:
sub/sub! == Find
gsub/gsub! == Find and Replace
scan == Find All
Hope that helps.
James Edward Gray II
That does help, James. Thanks. But, don't "sub" and "sub!" do a
replacement; so, they're doing more than just finding? At least, they're
finding and replacing the first instance of whatever, just not globally?
in fairness to perl i'd say this is probably true. on the otherhand i expect
that the ruby community will continue to be kind as a result of the current
community sense of politeness and our desire for a comfortable place to get
ruby help fast and with no bitter after taste. in summary i think it's up to
us to make sure this community doesn't follow the path of many large
communities: we don't have to accept that large means rude and anonymous.
kind regards.
-a
···
On Sat, 27 Jan 2007, Andy Lester wrote:
This is indeed the most generous of forums. I would've been flamed long
ago in the Perl world.
I suggest that it's not a feature of Perl per se, but of the larger number
of people in Perl. Given a large enough group, you'll always have a
percentage of people who are jerks. I've already run into people who've
treated me poorly because I was new and unfamiliar to them.
--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama
Ick. Sorry. In my defense I only got four hours of sleep last night.
Let me try my chart one more time:
sub/sub! == Find and Replace
gsub/gsub! == Replace All
scan == Find All
James Edward Gray II
···
On Jan 26, 2007, at 10:44 AM, Peter Bailey wrote:
James Gray wrote:
On Jan 26, 2007, at 8:45 AM, Peter Bailey wrote:
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.
gsub/gsub! also take a block.
Which one you use depends mainly on your purpose. Here's how I view
the methods, in terms of a "Find and Replace" feature set common to
so many programs:
sub/sub! == Find
gsub/gsub! == Find and Replace
scan == Find All
Hope that helps.
James Edward Gray II
That does help, James. Thanks. But, don't "sub" and "sub!" do a
replacement; so, they're doing more than just finding? At least, they're
finding and replacing the first instance of whatever, just not globally?