title: regular expression goes into infinite loop
body: <p>I am parsing (species) names of the form:</p>
  
  <pre><code>Parus Ater
  H. sapiens
  T. rex
  Tyr. rex
  </code></pre>
  
  <p>which normally have two terms (binomial) but sometimes have 3 or more. </p>
  
  <pre><code>Troglodytes troglodytes troglodytes 
  E. rubecula sensu stricto
  </code></pre>
  
  <p>I wrote</p>
  
  <pre><code>[A-Z][a-z]*\.?\s+[a-z][a-z]+(\s*[a-z]+)*
  </code></pre>
  
  <p>which worked most of the time but occasionally went into an infinite loop. It took some time to track down that it was in the regex matching and then I realised it was a typo and I should have written</p>
  
  <pre><code>[A-Z][a-z]*\.?\s+[a-z][a-z]+(\s+[a-z]+)*
  </code></pre>
  
  <p>which performs properly. </p>
  
  <p>My questions are:</p>
  
  <ul>
  <li>why does this loop happen?</li>
  <li>is there a way I can check for similar regex errors before running the program? Otherwise it may be difficult to trap them before the prgram is distributed and cause problems.</li>
  </ul>
  
  <p>[Note: I don't need a more general expression for species - there is a formal 100+ line regex specification for Species names - this was just an initial filter]. </p>
  
  <p>NOTE: The problem arose because although most names were extracted precisely into 2 or occasionally 3/4 terms (as they were in italics) there were a few false positives (like <code>"Homo sapiens lives in big cities like London"</code>) and the match fails at "L".]</p>
  
  <p>NOTE: In debugging this I have found that the regex was often completing but being very slow (e.g. on shorter target strings). It is valuable that I found this bug through a pathological case. I have learnt an important lesson!</p>