Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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 drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-10-25 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (148 votes), past polls