http://qs321.pair.com?node_id=101878

Boudicca has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, noble monks -- I have a quandry:

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!