title: Writing a regex to capture text between outer parenthesis
body: <p>So I'm trying to a parse a file that has text in this format:</p>
  
  <pre><code>outerkey = (innerkey = innervalue)
  </code></pre>
  
  <p>It gets more complex.  This is also legal in the file:</p>
  
  <pre><code>outerkey = (innerkey = (twodeepkey = twodeepvalue)(twodeepkey2 = twodeepvalue2))
  </code></pre>
  
  <p>So I want to basically capture only the outerkey's text.  I cannot guarantee that all of the text will be on one line.  It is possible that the value be on multiple lines.  And there is more than one item in the file.</p>
  
  <p>So here's my regex so far:</p>
  
  <pre><code>[^\s=]+\s*=\s*(\(\s*.*\s*\))
  </code></pre>
  
  <p>The goal is for me to simply replace the first part <code>[^\s=]+</code> with the key I want to search on and I get the entire text of the outer parenthesis.</p>
  
  <p>Here's the problem.  My regex will not only capture the text I want to capture, but it will also capture the text from the next group since regex's are greedy.  Making it not greedy would not work either since it will stop capturing at the first closing parenthesis.</p>
  
  <p>Ultimately, if I have the following string</p>
  
  <pre><code>foo = 
  (
    ifoo = ifoov
  )
  
  bar =
  (
    ibar =
      (iibar = iibarv)
      (iibar2 = iibarv2)
  )
  </code></pre>
  
  <p>I want the groups to match </p>
  
  <pre><code>(
    ifoo = ifoov
  )
  </code></pre>
  
  <p>and </p>
  
  <pre><code>(
    ibar =
      (iibar = iibarv)
      (iibar2 = iibarv2)
  )
  </code></pre>
  
  <p>Right now it will match</p>
  
  <pre><code>(
    ifoo = ifoov
  )
  
  bar =
  (
    ibar =
      (iibar = iibarv)
      (iibar2 = iibarv2)
  )
  </code></pre>
  
  <p>By the way, I am running this in multiline and singleline mode.</p>
  
  <p>Any ideas?  Thanks!</p>