title: Python regex to validate expression
body: <p>I'm trying to check the validity of some expressions using a Python regex. <br>
  Expressions could be like these ones:</p>
  
  <pre><code>(expand(function_1(&lt;&lt;TAG_1&gt;&gt;)) or expand(function_2(&lt;&lt;TAG_2&gt;&gt;))) 
                                                              and tag_value(&lt;&lt;TAG_3&gt;&gt;) &gt;= 3
  </code></pre>
  
  <p>What I need to validate is that all expression is valid (means that it has a correct number of parenthesis, all arithmetical operators are present and so on) and especially, that inside <em>expand</em> function there is only one argument; <br>
  means that the following example is wrong:</p>
  
  <pre><code>(expand(function_1(&lt;&lt;TAG_1&gt;&gt;) and function_3(&lt;&lt;TAG_4&gt;&gt;)) or 
                                expand(function_2(&lt;&lt;TAG_2&gt;&gt;))) and tag_value(&lt;&lt;TAG_3&gt;&gt;) &gt;= 3
  </code></pre>
  
  <p>To do that, I wrote this Python regex:</p>
  
  <pre><code>expand\s*\(\s*(\&lt;\&lt;(.+)\&gt;\&gt;\s*[\+-/\*\&lt;\&gt;\=\s*0-9]+|([A-Za-z0-9_]+)\s*\(\s*(\&lt;\&lt;(.+)\&gt;\&gt;|\&lt;\&lt;(.+)\&gt;\&gt;\s*,\s*.*)\))\s*\)
  </code></pre>
  
  <p><a href="https://www.debuggex.com/r/s5OYO4WmMB_l5ExU" rel="nofollow noreferrer">Debuggex Demo</a></p>
  
  <p>As you can see from the debuggex demo, the string used as test is not valid</p>
  
  <pre><code>(expand( function_1(&lt;&lt;TAG_1&gt;&gt;) and &lt;&lt;pippo&gt;&gt; &lt; 3) or 
             expand(function_2(&lt;&lt;TAG_2&gt;&gt;))) and tag_value(&lt;&lt;TAG_3&gt;&gt;) &gt;= 3
  </code></pre>
  
  <p>More in details, seems that it takes from the first <em>expand</em> until the last one.</p>
  
  <p>How is it possible fix the regex to achieve this goal?</p>
  
  <p>Thank you in advance </p>