title: Perl regex to capture group and stop matching
body: <p>I need some help with this perl regular expression</p>
  
  <pre><code>s/.*?&lt;\?lsmb if\s*?(\S*)\s*?\?&gt;/$1/
  </code></pre>
  
  <p>in the code below parsing out some non-whitespace chars <code>[A-Z][a-z][0-9][_]</code> surrounded by any number of whitespace and the other chars. I have tried various Perl regular expressions which are all commented out in the program below.</p>
  
  <p>My main problem I think is stopping matching at the end.</p>
  
  <p>The code below runs 8 tests, and I am hoping to find something that passes all 8.</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  use diagnostics;
  
  my $count = 0;
  my $t = 0;
  #examples of things I need to match, match =&gt; catagory
  my $self = {  'customerfax' =&gt; 'alpha',
               '_____' =&gt; 'Underscore',
               '000000' =&gt; 'numeric',
               'letter_reason_4' =&gt; 'alfa-numeric-underscore',
               'customerphone7' =&gt; 'alfa-numeric', 
               'customer_phone' =&gt; 'alfa-underscore',
             };
  # must contain   &lt;?lsmb 'varname from $self' ?&gt; 
  # may contain any amount of whitespace chars where one is depected
  # will end with \n that is removed by chop below         
  my $test1 = qq|&lt;?lsmb if customerfax ?&gt;  caacaacac\n|;
  my $test2 = qq|&lt;?lsmb if _____ ?&gt; bbb\n|;
  my $test3 = qq|&lt;?lsmb if 000000 ?&gt; cccc\n|;
  my $test4 = qq|&lt;?lsmb if letter_reason_4 ?&gt;&lt;t \&gt;&lt;/'&gt;&lt;&gt;&lt;&gt;&lt;&gt;\n|;  # /
  my $test5 = qq| &lt;?lsmb if customerfax ?&gt; |;
  my $test6 = qq|&lt;?lsmb if  customerphone7   ?&gt; \&lt;?lsmb ignore this &gt;n|;
  my $test7 = qq|&lt;?lsmb if  customer_phone  ?&gt;\n|;
  my $test8 = qq| avcscc 34534534 &lt;?lsmb if letter_reason_4 ?&gt; 0xffff\n|;
  
  strip_markup($test1);
  strip_markup($test2);
  strip_markup($test3);
  strip_markup($test4);
  strip_markup($test5);
  strip_markup($test6);
  strip_markup($test7);
  strip_markup($test8);
  
  if ($count == 8) { print "Passed All done\n";}
  else { print "All done passed  $count out of 8 Try again \n"; }
  
  
  sub strip_markup { 
      $_= shift;
      #print "strip_markup $_ \n";
      if (/&lt;\?lsmb if /) {
          chop; # gets rid ot the new line
          #original
          #s/.*?&lt;\?lsmb if (.+?) \?&gt;/$1/;
          #What I have tried:
          #s/.*?&lt;\?lsmb if(?:\s)*?(\S+?)(?:\s)*?\?&gt;\b/$1/;
          s/.*?&lt;\?lsmb if\s*?(\S*)\s*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s*?([A-Za-z0-9_]*?)\s*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if[\s]*?(\S*?)[\s]*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if (\S*?) \?&gt;/$1/;
          #s/.*?&lt;\?lsmb if (\S+?) \?&gt;/$1/;
          #s/.*?&lt;\?lsmb if ([\S]+?)([\s]+?)\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if[\s]+([\S]+)[\s]+\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s*?([\S]*?)\s*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s+?([\S]+?)[\s]+?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if ([\S]+?) \?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s*?([\S_]*?)\s*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s*?([[a-zA-Z]|[\d]|[_]]*?)\s*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s*?([a-zA-Z\d_]*?)\s*?\?&gt;/$1/;
          #s/.*?&lt;\?lsmb if\s*?([^[:space:]]+?)\s*?\?&gt;/$1/;
  
          $t++;
          print "Test $t ";
          #look up the result as the hash key
          my $ok = $self-&gt;{$_};
          if ($ok) { 
                  $count++;
                  print "OK passed $ok,";
          }
          print qq|Test Value : '$_' \n|;
      }
  }
  </code></pre>
  
  <p>Here are some of the Tests and what they should return: </p>
  
  <ul>
  <li>Test1 = <code>&lt;?lsmb if customerfax ?&gt;  caacaacac\n</code> should return <code>customerfax</code></li>
  <li>Test2 = <code>&lt;?lsmb if _____ ?&gt; bbb\n</code> should return   <code>_____</code></li>
  <li>Test8 =  <code>avcscc 34534534 &lt;?lsmb if letter_reason_4 ?&gt; 0xffff\n</code>
  should return <code>letter_reason_4</code></li>
  </ul>