M
Mintcake
I wouldd be grateful to anyone who can shed some light on the
unexpected
results from the regex in the following program.
#!/usr/local/bin/perl -l
use strict;
my $y = ' href="/foo/bar?d=1&c=2&f=1&cards=1" x="123"';
for ($y =~ /(\s+\w+=['"](.*?)["'])/gs)
{
print "1) $_";
print "2) [$1][$2]";
my $x = /(\w+)=['"](.*)["']/;
print "3) [$x] [$1][$2]";
my $x = /(\w+)=['"](.*)["']/;
print "4) [$x] [$1][$2]";
my $x = /(\w+)=['"](.*)["']/;
print "5) [$x] [$1][$2]";
print "";
}
__END__
The results I get are as follows
1) href="/foo/bar?d=1&c=2&f=1&cards=1"
2) [ x="123"][123]
3) [1] [href][/foo/bar?d=1&c=2&f=1&cards=1]
4) [1] [href][/foo/bar?d=1&c=2&f=1&cards=1]
5) [1] [href][/foo/bar?d=1&c=2&f=1&cards=1]
1) /foo/bar?d=1&c=2&f=1&cards=1
2) [href][/foo/bar?d=1&c=2&f=1&cards=1]
3) [] [href][/foo/bar?d=1&c=2&f=1&cards=1]
4) [] [href][/foo/bar?d=1&c=2&f=1&cards=1]
5) [] [=2&f=][]
1) x="123"
2) [=2&f=][]
3) [1] [x][123]
4) [1] [x][123]
5) [1] [x][123]
1) 123
2) [x][123]
3) [] [x][123]
4) [] [x][123]
5) [] [x][123]
Now I accept that this code is sloppy for several reasons but in my
defence I have
to say that it is not my code.
1. A while loop would probably be better than a foreach loop
2. The first regex is attempting to break the string in a list of
att="value" type
strings but is returning att="value" and "value" so the .*? should not
be parenthesized
3. No attempt is made to ensure that the same type of quote is used at
the start and
end of the value
The thing I cannot explain are the results from the second iteration
of the loop. The
same regex is executed three times and each time it fails (correctly),
however, the third
time the $1 and $2 values are overwritten. I have always believed
that the $digit variable
would be preserved if the regex failed to match. Reading the Camel
indicates that this
should indeed be the case.
No matter how many times the regex is executed within the loop it is
only on the final one
$1 and $2 are overwritten
unexpected
results from the regex in the following program.
#!/usr/local/bin/perl -l
use strict;
my $y = ' href="/foo/bar?d=1&c=2&f=1&cards=1" x="123"';
for ($y =~ /(\s+\w+=['"](.*?)["'])/gs)
{
print "1) $_";
print "2) [$1][$2]";
my $x = /(\w+)=['"](.*)["']/;
print "3) [$x] [$1][$2]";
my $x = /(\w+)=['"](.*)["']/;
print "4) [$x] [$1][$2]";
my $x = /(\w+)=['"](.*)["']/;
print "5) [$x] [$1][$2]";
print "";
}
__END__
The results I get are as follows
1) href="/foo/bar?d=1&c=2&f=1&cards=1"
2) [ x="123"][123]
3) [1] [href][/foo/bar?d=1&c=2&f=1&cards=1]
4) [1] [href][/foo/bar?d=1&c=2&f=1&cards=1]
5) [1] [href][/foo/bar?d=1&c=2&f=1&cards=1]
1) /foo/bar?d=1&c=2&f=1&cards=1
2) [href][/foo/bar?d=1&c=2&f=1&cards=1]
3) [] [href][/foo/bar?d=1&c=2&f=1&cards=1]
4) [] [href][/foo/bar?d=1&c=2&f=1&cards=1]
5) [] [=2&f=][]
1) x="123"
2) [=2&f=][]
3) [1] [x][123]
4) [1] [x][123]
5) [1] [x][123]
1) 123
2) [x][123]
3) [] [x][123]
4) [] [x][123]
5) [] [x][123]
Now I accept that this code is sloppy for several reasons but in my
defence I have
to say that it is not my code.
1. A while loop would probably be better than a foreach loop
2. The first regex is attempting to break the string in a list of
att="value" type
strings but is returning att="value" and "value" so the .*? should not
be parenthesized
3. No attempt is made to ensure that the same type of quote is used at
the start and
end of the value
The thing I cannot explain are the results from the second iteration
of the loop. The
same regex is executed three times and each time it fails (correctly),
however, the third
time the $1 and $2 values are overwritten. I have always believed
that the $digit variable
would be preserved if the regex failed to match. Reading the Camel
indicates that this
should indeed be the case.
No matter how many times the regex is executed within the loop it is
only on the final one
$1 and $2 are overwritten