Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: how to use string RE (against $_ and capture)

by AnomalousMonk (Archbishop)
on Nov 12, 2016 at 00:15 UTC ( [id://1175777]=note: print w/replies, xml ) Need Help??


in reply to Re: how to use string RE (against $_ and capture)
in thread how to use string RE (against $_ and capture)

... I'm not sure how the "{([0-6BS])}" will get interpreted as. "{" and "}" in a regex are normally used to quantify the number of matches.

Apparently, if the RE compiler can't see anything in the regex that looks like a count or count range, it just gives up on the whole counted quantifier thing and takes the curlies as literal characters.

c:\@Work\Perl>perl -wMstrict -le "$_ = 'xxqr{S}xx'; print qq{matched '$&', \$1 is '$1'} if m/qr{([0-6BS])}/; " matched 'qr{S}', $1 is 'S'
(Tested under ActiveState 5.8.9, Strawberry 5.14.4.1.)

To store a regex as a hash key, you could prefix the regex with a character that won't otherwise appear as the first character of any of your has keys.

I still prefer some sort of separate tag to distinguish pattern from exact matching:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my %options = ( qr{ x (Y) z }xms => { type => 'pattern', name => 'foo', }, '(?x) x (Y) z ' => { type => 'pattern', name => 'fum', }, 'xYzzy' => { type => 'exact', name => 'bar', }, 'zzzzz' => { type => '?????', name => 'zot', }, ); dd \%options; ;; $_ = 'xYzzy'; for my $p (sort keys %options) { my ($p_type, $p_name) = @{ $options{$p} }{ qw(type name) }; if ($p_type eq 'pattern') { if ($_ =~ $p) { print qq{'$p_name' pattern match of $p, \$1 is '$ +1'}; } } elsif ($p_type eq 'exact') { if ($_ eq $p) { print qq{'$p_name' exact match of '$p'}; } } else { die qq{unknown: $p; type '$p_type'; name '$p_name'}; } } " { "(?^msx: x (Y) z )" => { name => "foo", type => "pattern" }, "(?x) x (Y) z " => { name => "fum", type => "pattern" }, "xYzzy" => { name => "bar", type => "exact" }, "zzzzz" => { name => "zot", type => "?????" }, } 'foo' pattern match of (?^msx: x (Y) z ), $1 is 'Y' 'fum' pattern match of (?x) x (Y) z , $1 is 'Y' 'bar' exact match of 'xYzzy' unknown: zzzzz; type '?????'; name 'zot' at -e line 1.
(Test under same Perl versions as above.)


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-19 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found