<> wrote in comp.lang.perl.misc:
> Hal Vaughan wrote:
> > I've been reading the FAQ on Regexes and I see
> > I can map to create an array of patterns to be
> > found in a Regex, like this:
> >
> > @patterns = map { qr/\b$_\b/i } qw( foo bar baz );
> >
> > LINE: while( <> ) {
> > foreach $pattern ( @patterns ) {
> > print if /\b$pattern\b/i;
> > next LINE;
> > }
> > }
> >
> > I think I remember, at some point, reading how it
> > was possible to specify an array or hash in a regex
> > to see if one or more of multiple matches were
> > found, someting like:
> >
> > @pattern = qw(foo bar baz);
> > if ($line =~ /@pattern/) {print "Found a match!\n";}
>
>
> Try this instead:
>
> my $pattern = join '|', qw(foo bar baz);
> if ($line =~ /\b($pattern)\b/) {print "Found a match!\n";}
This is probably what the OP was up to, but it has some problems.
The test patterns should be mapped through quotemeta() in case the
strings contain regex metacharacters. Also, the parentheses around
$pattern should be non-capturing.
my $pattern = join '|', map quotemeta, qw( foo bar baz);
if ( $line =~ /\b(?:$pattern)\b/ ) { # etc.
It doesn't matter in this case, but if you care which string matched
it is often useful to sort the test strings into descending order
by length. If one string is a substring of another, that guarantees
that the bigger string is tried first.
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.