I expect "abc" =~ /\Aabc\z/gc; to be false, as /c is there,like in this case:
$pathname =˜ tr/a-zA-Z/_/cs; # change non-(ASCII)alphas to single underbar
But it turns out to be true, anyone can explain the reason?
See perlretut for what /c actually does (Applied to a regexp match operator it doesn't complement as it would with tr///). Transliteration is not a regexp, and regexen are not transliterations.
From perlretut:
A failed match or changing the target string resets the position. If you don't want the position reset after failure to match, add the //c , as in /regexp/gc .
Virtually the same passage appears in perlrequick.
perlre makes it a little harder to search for, but here's what it says:
g and c Global matching, and keep the Current position after failed matching. Unlike i, m, s and x, these two flags affect the way the regex is used rather than the regex itself. See Using regular expressions in Perl in perlretut for further explanation of the g and c modifiers.
/c does not do 'complement' matching :)
"abc" !~ /abc/; # false
To disallow abc anywhere in the string you could do
/((?!abc).)*\z/
but it would be much less efficient
EDIT:
/gc modifier: (from perldoc perlretut)A failed match or changing the target string resets the position. If you don't want the position reset after failure to match, add the
//c, as in/regexp/gc. The current position in the string is associated with the string, not the regexp. This means that different strings have different positions and their respective positions can be set or read independently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With