title: regex: balancing "{}" in a complex regex (python)
body: <p>I try to extract information from a complex string with regex. I try to extract what in the first <code>{</code> an last <code>}</code> as the content. Unfortunately, I struggle with nested <code>{}</code>. How is it possible to deal with this ?</p>
  <p>I think the key is to balance the <code>{}</code> over the all regex by I haven't been successful so far... See example below for parenthesis:
  <a href="https://stackoverflow.com/questions/546433/regular-expression-to-match-balanced-parentheses">Regular expression to match balanced parentheses</a></p>
  <pre><code>import re
  
  my_string = &quot;&quot;&quot;
  extend mineral Uraninite {
      kinetics {
          rate = -3.2e-08 mol/m2/s
          area = Uraninite
          y-term, species = Uraninite
          w-term {
              species = H[+]
              power = 0.37
          }
      }
      kinetics {
          rate = 3.2e-09 mol/m2/s
          area = Uraninite
          y-term, species = Uraninite
          w-term {
              species = H[+]
              power = 0.37
          }
      }
  }
  &quot;&quot;&quot;
  
  regex = re.compile(
          r&quot;extend\s+&quot;
          r&quot;(?:(?P&lt;phase&gt;colloid|mineral|basis|isotope|solid-solution)\s+)?&quot;
          r&quot;(?P&lt;species&gt;[^\n ]+)\s+&quot;
          r&quot;{(?P&lt;content&gt;[^}]*)}\n\s+}&quot;)
  extend_list = [m.groupdict() for m in regex.finditer(my_string)]
  </code></pre>
  <p>So far, I got:</p>
  <pre><code>print(extended_list[&quot;content&quot;])
  
  &quot;&quot;&quot;
      kinetics {
          rate = -3.2e-08 mol/m2/s
          area = Uraninite
          y-term, species = Uraninite
          w-term {
              species = H[+]
              power = 0.37
  &quot;&quot;&quot;
  </code></pre>
  <p>Appearly, I need to use the regex package <a href="https://pypi.org/project/regex/" rel="nofollow noreferrer">regex</a> because re does not support recursion. Indeed, this seems to work:</p>
  <pre><code>import regex as re
  pattern = re.compile(r&quot;{(?P&lt;content&gt;((?:[^{}]|(?R))*))}&quot;)
  extend_list2 = [m.groupdict() for m in pattern.finditer(read_data)]
  
  print(extended_list2[&quot;content&quot;])
  
  &quot;&quot;&quot;
  kinetics {
          rate = -3.2e-08 mol/m2/s
          area = Uraninite
          y-term, species = Uraninite
          w-term {
              species = H[+]
              power = 0.37
          }
      }
      kinetics {
          rate = 3.2e-09 mol/m2/s
          area = Uraninite
          y-term, species = Uraninite
          w-term {
              species = H[+]
              power = 0.37
          }
      }
  &quot;&quot;&quot;
  </code></pre>
  <p>But inserting it in the main pattern does not work.</p>
  <pre><code>pattern = re.compile(
          r&quot;extend\s+([^n]*)&quot;
          r&quot;(?:(?P&lt;phase&gt;colloid|mineral|basis|isotope|solid-solution)\s+)?&quot;
          r&quot;(?P&lt;species&gt;[^\n ]+)\s+&quot;
          r&quot;{(?P&lt;content&gt;((?:[^{}]|(?R))*))\}&quot;)
  extend_list = [m.groupdict() for m in pattern.finditer(read_data)]
  </code></pre>