in reply to Matching a long list of phrases
One suggestion to increase maintainability would be to store the list of phrases in an array (which could be generated from a text file or even a database) and then build the regex on the fly.
my @phrases = ( 'phrase one', 'phrase two', 'a different phrase', 'yet another match', 'one more to match', 'single', 'word', 'matches' ); my $regex = join '|', map { "\Q$_\E" } @phrases; my $regex = qr($regex); if ($incoming{text} =~ /$regex/) { # ... }
Note the use of \Q .. \E to defang any metacharacters that might have sneaked into your list of phrases. I've also precompiled the regex (qr()) which may save some time if you're doing lots of matches.
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
|
|---|