title: Regex - match mathematical expressions inside parenthesis
body: <p>I want a regular expression that will match valid math expressions inside of a pair of parentheses that <strong>do not contain any parenthesis of their own</strong>.</p>
  
  <ul>
  <li>The input is pre-validated, and only contains valid mathematical expressions.</li>
  </ul>
  
  <p>For example, given the following input, the regex should match <code>3*4.5</code>:</p>
  
  <blockquote>
    <p>(1 + 2 + 3 + 5 * 9 + 2 + 3 + (3*4.5)) </p>
  </blockquote>
  
  <p>I wrote the following regular expression which matches the simple case of up to one mathematical operation inside parentheses:</p>
  
  <pre><code>(?:-?\()(-?\d*\.*\d*\s*[-\+\*/]?\s*-?\d*\.*\d*)(?:\))
  </code></pre>
  
  <p>It successfully matches <code>3*4.5</code>.</p>
  
  <p>However, since the number of mathematical operations is unknown, I need the solution to be more generic. Naively, I thought this would work (added the <code>+</code> after the capture group):</p>
  
  <pre><code> (?:-?\()((-?\d*\.*\d*\s*[-\+\*/]?\s*-?\d*\.*\d*)+)(?:\))
  </code></pre>
  
  <p>However, the result is a timeout. See <a href="http://regexr.com/3b3es" rel="nofollow">here</a>.</p>
  
  <p>The desired regex should return <strong>3 * 4.5 + 7 /3 * -2</strong> for the input below</p>
  
  <blockquote>
    <p>(1 + 2 + 3 + 5 * 9 + 2 + 3 + (3 * 4.5 + 7 /3 * -2)) </p>
  </blockquote>