in reply to Re: Finding out which of a list of patterns matched
in thread Finding out which of a list of patterns matched
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Finding out which of a list of patterns matched
by Not_a_Number (Prior) on Jan 22, 2006 at 15:55 UTC | |
by tirwhan (Abbot) on Jan 22, 2006 at 16:14 UTC | |
by Not_a_Number (Prior) on Jan 22, 2006 at 17:55 UTC |