Boudicca has asked for the wisdom of the Perl Monks concerning the following question:
Say you have a string and a few patterns to try matching, something like this (it's dinky, please forgive):
$string = "clintonesque";
$m1 = "Clinton";
$m2 = "Bush";
$m3 = "Reagan";
if ($string =~ m/($m1|$m2|$m3)/i) {
print "Got a match.\n";
} else {
print "No match.\n";
}
What I want to know is _which_ of those three things ($m1, $m2, $m3) it matched first. Can it be done, or am I delusional?
I should note that I'd like to put all of the match patterns into one monster regexp, instead of if/else-ing my way through a series of them. (Honestly, I don't know why; this was a suggestion from a more experienced engineer, in the interest of conserving computer juice with some pre-compiling.)
The FAQs/resources don't offer any aid, and other Perl wizards say it can't be done. I would be intensely grateful for any ideas...
Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Returning _which_ pattern matched...?
by bikeNomad (Priest) on Aug 03, 2001 at 08:06 UTC | |
Re: Returning _which_ pattern matched...?
by Hofmator (Curate) on Aug 03, 2001 at 13:47 UTC | |
Re: Returning _which_ pattern matched...?
by lestrrat (Deacon) on Aug 03, 2001 at 18:53 UTC | |
(dkubb) Re: (2) Returning _which_ pattern matched...?
by dkubb (Deacon) on Aug 03, 2001 at 20:33 UTC | |
Re: Returning _which_ pattern matched...?
by scain (Curate) on Aug 03, 2001 at 19:02 UTC |