in reply to Matching against list of patterns
There are a couple of ways to discover what pattern matched. The easiest is probably to use backrefs:
my $pattern = join '|', map "($_)", @patterns; ... if ($line =~ /$pattern/i) { my $matchref = $#-; print "matched $pattern[$matchref - 1]\n"; }
As warned by other posters, you may need to play with the ordering to ensure that the preferred pattern matches if more than one can, in which case you will also need a way to get back from the index of the matched backreference to the actual pattern.
Hugo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Matching against list of patterns
by Schuk (Pilgrim) on Nov 17, 2004 at 12:42 UTC | |
by hv (Prior) on Nov 18, 2004 at 13:07 UTC |
In Section
Seekers of Perl Wisdom