Folks,
Here's a weird regex behavior I'm getting that I don't understand.
Problem description: Look at the 2 "if (s/.../.../)" statements below
[marked (Case A) & (Case B)]. They are basically identical & each get
the same input. The only difference is that the 2nd regex has a "\s*" at
the start of the pattern that the first one doesn't.
In each case, the actual $_ substitution occurs fine. Case A seems to
behave as expected. However, in case B, somehow $1 & $2 lose their value
once inside the {}.
What gives?
Code and output is provided below.
Code:
----
#!/usr/local/bin/perl
print "(Case A)\n";
@strings= ("fooxyzbar 123", "fooabcde 123");
foreach (@strings)
{
if (s/(foo)(.+)\s+\d+/$2/g)
{
print "'$_' -> :$1:$2:\n";
}
}
print "\n\n";
print "(Case B)\n";
@strings= ("fooxyzbar 123", "fooabcde 123");
foreach (@strings)
{
if (s/\s*(foo)(.+)\s+\d+/$2/g)
{
print "'$_' -> :$1:$2:\n";
}
}
Output:
------
(Case A)
'xyzbar' -> :foo:xyzbar:
'abcde' -> :foo:abcde:
(Case B)
'xyzbar' -> :::
'abcde' -> :::
Thanks,
-Nilanjan
_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm