Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why complement searchlist doesn't work?

Tags:

perl

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?

like image 828
new_perl Avatar asked May 20 '26 15:05

new_perl


2 Answers

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.

like image 74
DavidO Avatar answered May 23 '26 07:05

DavidO


/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.

like image 31
sehe Avatar answered May 23 '26 05:05

sehe