in reply to First Pattern Matching

This is my solution. Its advantage is that it doesn't check which pattern that matched afterwards. Instead it registers which pattern that matched during the match.

my @strs = qw/ ABCBXBCA APCBXBCAC /; my @patterns = (qr/B.B/, qr/CB/); # See comment below. my $matched_pattern; my $all_pats = do { use re 'eval'; qr/(.*?)(?:@{[join '|', map "$patterns[$_](?{\$matched_pattern = $ +_})", 0 .. $#patterns]})/; }; foreach (@strs) { print "String:$_ Pattern:$patterns[$matched_pattern] KeyWord:$1\n" if /$all_pats/; }
I use qr// around all the patterns in the assignment to @patterns in this code. That is to avoid slip-ups involving \s and such (for double-quoted strings). However, doing that is a slight compile-time performance hit in this case since I never use the patterns directly, just indirectly. But I like to qr// them anyway, especially if it is an important piece of code. (And besides, you get a FREE and FUN non-capturing parenthesis around it! :))

Cheers,
-Anomo