Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: One line assigment statement with regex match

by mugwumpjism (Hermit)
on Jun 23, 2005 at 00:44 UTC ( [id://469254]=note: print w/replies, xml ) Need Help??


in reply to One line assigment statement with regex match

I tend to use something like this;

my $re = qr/(?:${\( join "|", map { qr/\Q$_\E/ } @terms )})/; ... my ($match) = ($line =~ m/($re)/);

This is especially important if your @terms contain metacharacters such as parantheses, asterix, period, etc.

Also, it is usually faster because you're only doing one regular expression match per line, not many.

$h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

Replies are listed 'Best First'.
Re^2: One line assigment statement with regex match
by ketema (Scribe) on Jun 23, 2005 at 15:37 UTC
    I get search pattern not terminated when I try this:
    my $re = qr/(?:${\( join "|", map { qr/\Q$_\E/ } @terms )})/;

      Terribly sorry, I didn't test it.

      Yes, the double use of "/" to delimit regular expressions is tripping up the parser. Much better to use braces;

      @terms=qw(foo bar b.*az); my $re = qr{(?:${\( join "|", map { qr{\Q$_\E} } @terms )})}; print "$re\n";

      The above will print "(?-xism:(?:(?-xism:foo)|(?-xism:bar)|(?-xism:b\.\*az)))"

      $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

Log In?
Username:
Password:

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

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

    No recent polls found