Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 meditating upon the Monastery: (4)
As of 2015-07-05 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls