SPUG: Strange regex problem (correction)
Riley
wormwood at speakeasy.org
Sat Sep 23 01:27:08 CDT 2000
> One last twist in this bug is that it only seems to happen when "*" and
> the case-insensitive flag are used together.
On second thought, the bug seems to occur not due to the presence of
"*" but whenever there are enough characters left after the last
successful match to possibly yield another match.
> perl -e'$foo = "abcdefghijklmnopqrstuvwxyz";
> ($matches) = ($foo =~ s/(hi)(.+)(stu)/\U$1\E$2\U$3\E/gi);
> print "$foo\n$1\n$matches\n"'
> abcdefgHIjklmnopqrSTUvwxyz
> hi
> 1
This doesn't trigger the bug because the "+" needs at least one character,
so the entire matching expression needs at least 6 characters -- one too
many. But if we give it enough characters, it will attempt another match
and clobber the substring variables:
perl -e'$foo = "abcdefghijklmnopqrstuvwxyz";
($matches) = ($foo =~ s/(hi)(.+)(nop)/\U$1\E$2\U$3\E/gi);
print "$foo\n$1\n$matches\n"'
abcdefgHIjklmNOPqrstuvwxyz
1
Another example without "+" or "*":
perl -e'$foo = "abcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaa";
($matches) = ($foo =~ s/(hi)(jklmnopqr)(stu)/\U$1\E$2\U$3\E/gi);
print "$foo\n$1\n$matches\n"'
abcdefgHIjklmnopqrSTUvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaa
1
The problem can be seen to affect (non-lazy) global matches as well
as substitutions:
perl -e'$foo = "abcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaa";
($matches) = ($foo =~ m/(hi)(jklmnopqr)(stu)/gi);
print "$foo\n$1\n$matches\n"'
abcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaa
hi
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
More information about the spug-list
mailing list