Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-12-28 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls