in reply to First Pattern Matching
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! :))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/; }
|
|---|