Closed p5pRT closed 18 years ago
This is a short test script showing what I believe to be a bug.
=====================begin script #!/usr/bin/perl
$regex = 'aeiou'; $compiled_regex = qr/$regex/;
$string = 'ae_ai_';
#should not match either string - the final regex should fail as it should match aeiou only\, not _
if ($string =~ /^([^\_]+)\_([$regex])([$regex])([$regex])$/) { print STDERR "PLAIN MATCHED '$&'\, '$1' and graphemes '$2'\, '$3' and '$4' \n"; }
if ($string =~ /^([^\_]+)\_([$compiled_regex])([$compiled_regex])([$compiled_regex])$/) { print STDERR "COMPILED MATCHED '$&'\, '$1' and graphemes '$2'\, '$3' and '$4'\, using $compiled_regex\n"; } ==================end script
As the comment says\, the variable $string should not match the regexes; it correctly does not match the uncompiled version\, but incorrectly does match the compiled version\, somehow matching underscore with the compiled 'aeiou'.
On Thu\, Nov 02\, 2006 at 06:01:50AM -0800\, sue @ inf. ed. ac. uk wrote:
if ($string =~ /^([^\_]+)\_([$regex])([$regex])([$regex])$/) {
$x = qr/.../; /...$x..../
Does not do what I guess you think it does. It doesn't insert a call to that compiled regex at that point; instead it inserts a string representation of that compiled regex; in particular:
$ perl588 -we'$r = qr/aeiou/; print "[$r]\n"' [(?-xism:aeiou)] $
Notice how the regex is expands to the string "(?-xism:aeiou)". The only time that this string expansion doesn't take place is when the variable is the whole string\, as in
$x =~ $regex or $x =~ /$regex/
So putting a compiled regex within a character class won't do the right thing.
-- SCO - a train crash in slow motion
The RT System itself - Status changed from 'new' to 'open'
sue @ inf . ed . ac . uk schreef:
if ($string =~ /^([^\_]+)\_([$regex])([$regex])([$regex])$/)
There is no need to escape the underscores. The $regex contains 'aeiou' which isn't a regex but a string of vowel characters. Use better names.
if ($string =~ /^([^\_]+)\_([$compiled_regex])([$compiled_regex]) ([$compiled_regex])$/) { print STDERR "COMPILED MATCHED '$&'\, '$1' and graphemes '$2'\, '$3' and '$4'\, using $compiled_regex\n"; } ==================end script
[$compiled_regex] can be the same as [cdegilmoprx$_] which contains [eio_].
But in this case\, [$compiled_regex] is [(?-xism:aeiou)]\, which contains the range "?-x"\, which contains a lot of characters.
-- Affijn\, Ruud
"Gewoon is een tijger."
@rgs - Status changed from 'open' to 'rejected'
Migrated from rt.perl.org#40648 (status was 'rejected')
Searchable as RT40648$