Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: regex with arrays and variables

by jwkrahn (Monsignor)
on Apr 25, 2013 at 18:19 UTC ( #1030713=note: print w/ replies, xml ) Need Help??


in reply to regex with arrays and variables

if ($i =~ m/@array2/) { print "$i is in array2 - skipping\n";

That regular expression is m/foo bar quux/ and english for that is "array2 is in $i - skipping\n".    It won't work because you are looking for the string "foo bar quux".



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

$regex now contains the string "(foo|bar|quux|)" which says to match EITHER "foo" OR "bar" OR "quux" OR "", and EVERY string will match "".

You need something like:

my $regex = "("; $regex .= join "|", @array2; $regex .= ")";


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://1030713]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2015-07-05 06:07 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