Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Compiled regular expression using qr

by AnomalousMonk (Archbishop)
on Oct 15, 2014 at 21:25 UTC ( [id://1103970]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Compiled regular expression using qr
in thread Compiled regular expression using qr

In the OP and in your reply above, you use the terms "exact" and "case insensitive" in the context of regex matching in a way that leads me to wonder if you understand that case insensitive matching is inherently inexact. E.g.:

c:\@Work\Perl>perl -wMstrict -le "my $rx = qr/^data_123$/i; print $rx; ;; for my $s (qw(data_123 DATA_123 dAtA_123 data_123x data_12 nada_123) +) { if ($s =~ $rx) { print qq{'exact' match to '$s'}; } else { print qq{NO match to '$s'}; } } " (?^i:^data_123$) 'exact' match to 'data_123' 'exact' match to 'DATA_123' 'exact' match to 'dAtA_123' NO match to 'data_123x' NO match to 'data_12' NO match to 'nada_123'

Update: I should have mentioned that the example above was run under Strawberry Perl version 5.14.4.1.

Replies are listed 'Best First'.
Re^4: Compiled regular expression using qr
by perlbaski (Sexton) on Oct 15, 2014 at 22:32 UTC
    My bad, I reinitialized the variable as part of debugging, no wonder it would not match.

    Thanks for clarifying on its use. That said, the stringify version has ?^ in front, I know that ? means 0 or more of the chars preceding it, but what does ?^ mean?

      A stringized regex object is wrapped in a non-capturing grouping that preserves the regex modifiers in effect when the regex was compiled. This allows a regex object to be unaffected by the modifiers in effect in any enclosing regex 'scope' during interpolation within another regex or regex object. Consider:

      c:\@Work\Perl>perl -wMstrict -le "my $rx = qr/(?^xms:^data_123$)/; print $rx; ;; my $ry = qr/^data_123$/xms; print $ry; ;; my $rz = qr{ T $rx U $ry V }xms; print $rz; " (?^:(?^xms:^data_123$)) (?^msx:^data_123$) (?^msx: T (?^:(?^xms:^data_123$)) U (?^msx:^data_123$) V )
      See also the discussion of  "(?^aluimsx:pattern)" in the Extended Patterns section of perlre.

      ?^ is garbage. (?^i:...), on the other hand, means that the pattern within should be matched case-insensitively as you requested.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-23 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found