in reply to Regex bug? (/u not cooperating with /x)

/x only impacts how the regex is parsed. It can't impact how the string is parsed. \u impacts how the string is generated. The string has to be parsed and generated before the resulting string can be passed to the regex engine which then parses the string as a regex. And it is clear that /x can't impact the parsing of the string because the string has to be parsed before Perl can even see the /x.

If we fix your "bug", then we should also have to fix this to work:

"Y" =~ /\u(?x: y)/ or die "\\u and (?x: ) don't cooperate";

So this is a bit of a restatement of other replies, perhaps, but \u isn't a regex construct, so it isn't impacted by regex flags.

You'll also note other things that aren't impacted by /x:

/$foo bar[5]/x; # Not the same as /$foobar[5]/ /\ u2/x; # Not the same as /\u2/, obviously /\01 2/x; # Not the same as /\012/ /$hash{a Key}/x;# Not the same as /$hash{aKey}/

You can also tell that /x impacts regex parsing not string parsing because:

#!/usr/bin/perl -w my $x= " b c "; print for "abcd" =~ /$x/xg; # prints "bc"

And one other way to see that \u isn't a regex thingy:

print "($1)($2)\n" while "fFFggGGGG" =~ /([a-z]+)\U(\1*)/g; print "---\n"; print "($1)($2)\n" while "fFFggGGGG" =~ /([a-z]+)\U(\1*g)/g; __END__ Prints: (f)() (gg)() --- (gg)(G)

- tye        

Replies are listed 'Best First'.
Re^2: Regex bug? (/u not cooperating with /x) (string vs regex)
by mrpeabody (Friar) on Oct 24, 2007 at 05:49 UTC
    I see:
    print "($1)($2)\n" while "ggGGGG" =~ /([a-z])\U(\1*g)/g; # (g)(gG)

    \U applies to "\1", not to the "g" that it matches.

    So string parsing and regex parsing are two separate stages, the operators of which look similar but take effect at different times. That makes sense. I do think that a quick note in perlre would make it less surprising.