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
In reply to Re: Matching a long list of phrases
by davorg
in thread Matching a long list of phrases
by Hagbone
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |