title: Bash regular expression execution hangs on long expressions
body: <p>I need to validate a 38 field comma seperated string. Fields can be numeric, decimal or empty allowed strings.</p>
  
  <p>Problem is when I construct a regular expression for 38 fields and try to execute, it hangs forever and it hangs.</p>
  
  <p>I use following per field reg exps:</p>
  
  <pre><code>INT="[0-9]+"
  TIM="[0-9]+"
  NUM="[0-9]+(\.[0-9]+)?"
  STR=".*"  # --&gt; (also tried "[^,]*" but no change)
  </code></pre>
  
  <p>I constructed my regexps with above expressions.</p>
  
  <p><strong>1)</strong> This is working fine: (Output: "matches")</p>
  
  <pre><code>[[ "str1,1.1,5,6,7,8,9,str2,str3,str4,str1,1.1,5,6,7,8,9,str2,str3,str4,str1,1.1,5,6,7,8,9,str2,str3,str4,str1,1.1,5,6,7,8,9,str2,str3,str4" =~ ^.*\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,.*\,.*\,.*\,.*\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,.*\,.*\,.*\,.*\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,.*\,.*\,.*\,.*\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,.*\,.*\,.*$ ]] &amp;&amp; echo matches
  </code></pre>
  
  <p><strong>2)</strong> This hangs and execution wont complete !!!:</p>
  
  <pre><code>[[ "str1,1.1,5,6,7,8,9,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,2.0,str12,0.0,5.0,str13,12312545645,45456456478,78979754545,12312545645,45456456478,78979754545,78979754545,4.74,0.1245,4.174,0.4245,6,80,str14,str15" =~ ^.*\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,.*\,.*\,.*\,.*\,.*\,.*\,.*\,.*\,.*\,.*\,[0-9]+(\.[0-9]+)?\,.*\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,.*\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+(\.[0-9]+)?\,.*\,.*$ ]] &amp;&amp; echo matches
  </code></pre>
  
  <p>I thought <code>.*</code> is too generic then tried <code>[^,]*</code> but nothing changed.</p>
  
  <p>Please advice how can I solve this without splitting by "," once then compare one by one.</p>
  
  <hr>
  
  <h1>!!! Correction !!!</h1>
  
  <p>Above I stated:</p>
  
  <blockquote>
    <p>STR=".<em>"  # --> (also tried "[^,]</em>" but no change) </p>
  </blockquote>
  
  <p>This is wrong. Noticed that, I failed to replace all of them. When I replace all <code>.*</code> to <code>[^,]</code> problem is resolved. See below:</p>
  
  <p><strong>3)</strong>  This is fixed version and working as expected:</p>
  
  <pre><code>[[ "str1,1.1,5,6,7,8,9,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,2.0,str12,0.0,5.0,str13,12312545645,45456456478,78979754545,12312545645,45456456478,78979754545,78979754545,4.74,0.1245,4.174,0.4245,6,80,1,str15,str16" =~ ^[^,]*\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[^,]*\,[0-9]+(\.[0-9]+)?\,[^,]*\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[^,]*\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[0-9]+(\.[0-9]+)?\,[0-9]+\,[0-9]+\,[0-9]+(\.[0-9]+)?\,[^,]*\,[^,]*$ ]] &amp;&amp; echo matches
  </code></pre>
  
  <p>Watch out for <a href="http://www.rexegg.com/regex-explosive-quantifiers.html" rel="nofollow">Catastrophic Backtracking</a> that I learned from this issue.</p>