G
growlatoe
Hi all.
I'm pretty new to Ruby and that sort of thing, and I'm having a few
problems understanding regular expressions. I'm hoping one of you can
point me in the right direction.
I want to replace an entire string with another string. I know you
don't need regular expressions for that, but it's part of a more
generic approach. Anyway, the problem I'm having is that my regular
expressions are finding two matches instead of one, and I don't
understand why. I've narrowed down my confusion to the following code,
which shows some output from irb:
irb(main):001:0> "hello".scan(/.*/)
=> ["hello", ""]
I was expecting one match, not two, because .* matches everything,
right? Can someone explain why an empty string is also matched?
The same thing can be seen when substituting - this is closer to how
I'm using regular expressions in my code:
irb(main):001:0> "hello".gsub(/.*/, "P")
=> "PP"
Two substitutions are made and I expected one. So am I right or wrong
to expect one substitution?
Please help - this is driving me nuts!
And in case it helps...
$ ruby --version
ruby 1.8.5 (2006-08-25) [i486-linux]
Thanks in advance.
I'm pretty new to Ruby and that sort of thing, and I'm having a few
problems understanding regular expressions. I'm hoping one of you can
point me in the right direction.
I want to replace an entire string with another string. I know you
don't need regular expressions for that, but it's part of a more
generic approach. Anyway, the problem I'm having is that my regular
expressions are finding two matches instead of one, and I don't
understand why. I've narrowed down my confusion to the following code,
which shows some output from irb:
irb(main):001:0> "hello".scan(/.*/)
=> ["hello", ""]
I was expecting one match, not two, because .* matches everything,
right? Can someone explain why an empty string is also matched?
The same thing can be seen when substituting - this is closer to how
I'm using regular expressions in my code:
irb(main):001:0> "hello".gsub(/.*/, "P")
=> "PP"
Two substitutions are made and I expected one. So am I right or wrong
to expect one substitution?
Please help - this is driving me nuts!
And in case it helps...
$ ruby --version
ruby 1.8.5 (2006-08-25) [i486-linux]
Thanks in advance.