<?xml version="1.0" encoding="windows-1252"?>
<node id="391416" title="Matching against list of patterns" created="2004-09-16 07:50:20" updated="2005-08-11 07:32:45">
<type id="115">
perlquestion</type>
<author id="272682">
Eyck</author>
<data>
<field name="doctext">
&lt;p&gt;Hi, I find myself writting another app this month that requires matching against list of patterns, what I did the last time is:
&lt;code&gt;
#...
while ($line=&lt;&gt;) {
#...
  foreach $user (keys(%$users)) {
    my $pattern=$user-&gt;{'Pattern'};
    if ($line=~/$pattern/i) {
     print STDERR "Great, we found pattern $pattern!\n";
    }
  };
};

&lt;/code&gt;
&lt;p&gt;
This is straitghtforwad and it works.
&lt;p&gt;The problem is that it grows roughly &lt;tt&gt;N**2&lt;/tt&gt;, 
(because there is relation between number of lines to parse, and amount of possible patterns).
&lt;p&gt;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. 

&lt;p&gt; 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 .. )
&lt;p&gt; This seems like rather common task, are there any smart
(or at least working) solutions?</field>
</data>
</node>
