http://www.perlmonks.org?node_id=391416

Eyck has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I find myself writting another app this month that requires matching against list of patterns, what I did the last time is:

#... while ($line=<>) { #... foreach $user (keys(%$users)) { my $pattern=$user->{'Pattern'}; if ($line=~/$pattern/i) { print STDERR "Great, we found pattern $pattern!\n"; } }; };

This is straitghtforwad and it works.

The problem is that it grows roughly N**2, (because there is relation between number of lines to parse, and amount of possible patterns).

UPDATE: I keep patterns in hashref because the whole point of this excercise is to figure out WHICH regexp matched. ORing patterns together might be fine if I knew how to figure out which patter from such ORed pattern matched exactly.

What do people do with such problems? Is it possible to optimise this? ( for example, group patterns into eee...groups based on common prefix or something common, or maybe preprocess those lines, split sentences into functional parts .. )

This seems like rather common task, are there any smart (or at least working) solutions?