Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Why does /i not seem to work

by Eily (Monsignor)
on Nov 21, 2019 at 15:49 UTC ( #11108994=note: print w/replies, xml ) Need Help??


in reply to Why does /i not seem to work

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.

Replies are listed 'Best First'.
Re^2: Why does /i not seem to work
by AnomalousMonk (Bishop) on Nov 21, 2019 at 16:14 UTC
    ... 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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11108994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2020-06-07 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?