In your example, $#- will always be 1, because the conditional is fulfilled after the first match. Also, $#- can't really be useful in this matter at all, because it returns the index of the last match in @- ,which in turn gives you the match offset into the string, not the matched content.
Update: This is completely wrong and nobull is correct, my apologies (hangs head in shame). To make up for it, here's a practical example of finding your matched word with nobull's method (I still like my method below better, but YMMV):
my @words = qw(foo bar baz); my $regex = join(")|(",grep { quotemeta($_) } @words); if ($input =~ m/($regex)/ ){ my $index = $#- -1; print "matched word $words[$index]\n"; }
In reply to Re^2: Finding out which of a list of patterns matched
by tirwhan
in thread Finding out which of a list of patterns matched
by lemnisca
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |