Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: regex with arrays and variables

by moritz (Cardinal)
on Apr 25, 2013 at 18:22 UTC ( #1030715=note: print w/ replies, xml ) Need Help??

in reply to regex with arrays and variables

my $regex = "("; foreach (@array2) { $regex .= $_ . "|"; } $regex .= ")";

You're nearly there. The problem with this code is that it produces something like (foo|bar|quox|), and the trailing pipe symbol makes the empty string match. To fix that, use

my $regex = join '|', @array2;

If you don't want regex meta characters in @array2 to act specially, you should even write

my $regex = join '|', map quotemeta, @array2;

Comment on Re: regex with arrays and variables
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2015-10-10 07:45 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (255 votes), past polls