title: Match Parenthetical Expression With Regular Expressions
body: <p>I am working on a math expression parser using regular expressions and I am trying to add support for parentheses.</p>
  
  <p>My parser works like this:</p>
  
  <pre><code>function parse_expression(expression){
      Find parenthetical expressions
      Loop through parenthetical expressions, call parse_expression() on all of them
      Replace parenthetical expression with value of expression
      Find value of expression
      Return value
  }
  </code></pre>
  
  <p>Because it it recursive, I need to find only the outmost parenthetical expressions. For example if I was parsing the string "(5 + (4 + (3 / 4) + (3 * 2) + 2)) + (1 + 2)", I want to find the expressions "5 + (4 + (3 / 4) + (3 * 2) + 2)" and "1 + 2". How do you do this with Regular Expressions?</p>
  
  <p>The regular expression I have now ( "\(([^\)]+)\)" ) would return just "5 + ( 4 + ( 3 * 2", it doesn't get the full first expression and it gets none of the second.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks,</p>
  
  <p>Kyle</p>