in reply to Mathching an array in regex
this will show you#!/usr/bin/perl use strict; use warnings; my @arr=('cool','guy','here'); my $str1="I am cool"; if($str1 =~ m/@arr/){ print "Matched"; }
Note that /@arr/ is taken literally - it so has to be, only scalars are interpolated inside regexes, not arrays. You might try something like$ perl -MO=Deparse test.pl use warnings; use strict 'refs'; my(@arr) = ('cool', 'guy', 'here'); my $str1 = 'I am cool'; if ($str1 =~ /@arr/) { print 'Matched'; } test.pl syntax OK
but I'd prefere the solution with a loop/map based on the given array.#!/usr/bin/perl use strict; use warnings; my @arr=('cool','guy','here'); my $joined_rx = join( '|', map { quotemeta($_) } @arr ); my $str1="I am cool"; if ($str1 =~ m/$joined_rx/) { print "Matched"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Mathching an array in regex
by narainhere (Monk) on Oct 12, 2007 at 12:21 UTC | |
by Krambambuli (Curate) on Oct 12, 2007 at 12:46 UTC |