Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Why does /i not seem to work

by perlpipe (Acolyte)
on Nov 21, 2019 at 15:31 UTC ( [id://11108989]=perlquestion: print w/replies, xml ) Need Help??

perlpipe has asked for the wisdom of the Perl Monks concerning the following question:

my $r="abc|xyz"; my $rx=qr($r); my $c="XYZ"; my $x1=($c=~/$rx/i); my $x2=(lc($c)=~/$rx/i); say "\$x1=$x1"; say "\$x2=$x2";

Results in:

$x1=

$x2=1

Shouldn't the /i give us a case insensitive match?

Perl version: This is perl 5, version 28, subversion 0 (v5.28.0) built for MSWin32-x64-multi-thread

Running on Windows 10

Am I missing something obvious>

TIA

Replies are listed 'Best First'.
Re: Why does /i not seem to work
by choroba (Cardinal) on Nov 21, 2019 at 15:47 UTC
    Try examining the $rx:
    (?^:abc|xyz)

    The sequence ?^ is described in perlre:

    > Starting in Perl 5.14, a "^" (caret or circumflex accent) immediately after the "?" is a shorthand equivalent to "d-imnsx". Flags (except "d") may follow the caret to override it. But a minus sign is not legal with it.

    -i means the qr() by default creates case sensitive regex objects.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Why does /i not seem to work
by Eily (Monsignor) on Nov 21, 2019 at 15:49 UTC

    You can try this: say $rx to see what $rx is actually interpreted as. Spoiler, you'll see this:(?^u:abc|xyz).

    As documented in Extended Patterns:

    Starting in Perl 5.14, a "^" (caret or circumflex accent) immediately after the "?" is a shorthand equivalent to d-imnsx. Flags (except "d") may follow the caret to override it. But a minus sign is not legal with it.
    The ^ actually disables case insensitivity. That's because the qr operator "remembers" the flags used when creating a value with it, so that you can use it to always mean the same thing, regardless of where it's used.

    Three ways to solve your issue, use the /i when creating $rx: $rx=qr/$rx/i;. Use lc like you already did. Or do not use qr and keep the regex as a string.
    But that last one has some disavantages, using qr// will tell you if your regex is invalid sooner (if you use the regex in a completely different place, you might want the error message to be close to the definition, not the use), and it's not a bad thing to have something that is meant to be used as a regex to have the "type" Regexp.

      ... keep the regex as a string ... has some disavantages ...

      Not least of which is that a Regexp object interpolates into another regex as an independent, quantifiable non-capturing group, and an interpolated string does not:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $rs = 'abc|xyz'; my $rx = qr(abc|xyz); ;; my $regex = qr( $rs+ $rx+ )x; print $regex; " (?x-ism: abc|xyz+ (?-xism:abc|xyz)+ )
      The two different representations can have very different effects when interpolated into a larger regex.


      Give a man a fish:  <%-{-{-{-<

Re: Why does /i not seem to work
by LanX (Saint) on Nov 21, 2019 at 15:45 UTC
    are you aware that the regex object has it's own flags?
    DB<3> $r="abc|xyz"; DB<4> $rx=qr($r); DB<5> p $rx (?^u:abc|xyz)
    DB<6> $rx=qr(/$r/i); # strike it, see update DB<7> p $rx (?^u:/abc|xyz/i)

    update

    Sorry line 6 had the wrong syntax

    DB<27> $rx = qr($r)i; # or traditionally qr/$r/i DB<28> p $rx (?^ui:abc|xyz) DB<29> p "XYZ" =~ $rx 1

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Why does /i not seem to work
by perlpipe (Acolyte) on Nov 21, 2019 at 22:53 UTC

    Thanks everyone. That was very informative.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11108989]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-24 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found