title: In Perl, how to match several prefixes
body: <p>I have 2 input files.  One is a list of prefix and lengths, like this:</p>
  
  <pre><code>101xxx
  102xxx
  30xx
  31xx
  </code></pre>
  
  <p>(where x is any number)</p>
  
  <p>And another is a list of numbers.</p>
  
  <p>I want to iterate through the second file, matching each number against any of the prefix/lengths.  This is fairly easy.  I build a list of regexps:</p>
  
  <pre><code>my @regexps = ('101...', '102...', '30..', '31..');
  </code></pre>
  
  <p>Then:</p>
  
  <pre><code>foreach my $regexp (@regexps) {
      if (/$regexp/) {
          # do something
  </code></pre>
  
  <p>But, as you can guess, this is slow for a long list.</p>
  
  <p>I could convert this to a single regexp:</p>
  
  <pre><code>my $super_regexp = '101...|102...|30..|31..';
  </code></pre>
  
  <p>...but, what I need is to know <em>which</em> regexp matched the item, and what the ..s matched.</p>
  
  <p>I tried this:</p>
  
  <pre><code>my $catching_regexp = '(101)(...)|(102)(...)|(30)(..)|(31)(..)';
  </code></pre>
  
  <p>but then I don't know whether to look in $1, $3, %5 or $7.</p>
  
  <p>Any ideas?  How can I match against any of these prefix/lengths and know which prefix, and what the remaining digits where?</p>