I have been working on an expression using String#sub and am getting
inaccurate results on the first time I run the expression, but not on the
following times I run it. Is there something wrong with my regular
expression?
The intent of my regular expression is to remove preceding spaces and
apostrophes.
and that works on the first try... but I was just wondering why the previous
example does not work.
Thanks,
Trish
···
On 1/3/07, Trish Ball <trish.ball@gmail.com> wrote:
I have been working on an expression using String#sub and am getting
inaccurate results on the first time I run the expression, but not on the
following times I run it. Is there something wrong with my regular
expression?
The intent of my regular expression is to remove preceding spaces and
apostrophes.
and that works on the first try... but I was just wondering why the previous
example does not work.
Thanks,
Trish
I have been working on an expression using String#sub and am getting
inaccurate results on the first time I run the expression, but not on the
following times I run it. Is there something wrong with my regular
expression?
The intent of my regular expression is to remove preceding spaces and
apostrophes.
What's happening is that you're sort of inter-leafing (-leaving?) the
setting and using of $2. The $2 you use in the first replacement
string is nil; in that context, it won't pick up the match you just
did.
Here's a simpler example, for illustration:
irb(main):002:0> "abc".sub(/(a)/, "#$1")
=> "bc"
# $1 was nil, so the 'a' got deleted. But *now* $1 is 'a' (from the
# line that was just executed). Therefore...
irb(main):003:0> "abc".sub(/(a)/, "#$1")
=> "abc"
# ...this time, 'a' gets replaced with 'a'.
David
···
On Thu, 4 Jan 2007, Trish Ball wrote:
On 1/3/07, Trish Ball <trish.ball@gmail.com> wrote: