Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030715]
help
Chatterbox?
[marto]: I don't think their inability to make any sense is related to alcohol
[Discipulus]: their?
[karlgoethebier]: marto: "their inability.." Perhaps the result of a wrong education?
[karlgoethebier]: or no education?
[choroba]: their = his or her

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-05-23 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?