title: Handling Nested Regular Expression
body: <p>I am learning regex with experimenting on HTML files, I have a regex problem, </p>
  
  <p>My text is :</p>
  
  <pre><code>text='12&lt;a&gt;1&lt;a&gt;2&lt;/a&gt;3&lt;/a&gt;13&lt;a&gt;4&lt;a&gt;5&lt;/a&gt;6&lt;/a&gt;14'    
  </code></pre>
  
  <p>The expression </p>
  
  <pre><code>&lt;a&gt;&lt;a&gt;&lt;/a&gt;&lt;/a&gt; is nested
  </code></pre>
  
  <p>I want to write a regex that can handle nested expression, for example, my output for the above text should be </p>
  
  <pre><code>Output :    121314
  </code></pre>
  
  <p>I use the regex,</p>
  
  <pre><code>re.sub('&lt;a&gt;(.+?)&lt;/a&gt;', '', text, flags=re.DOTALL)
  </code></pre>
  
  <p>I get an output:</p>
  
  <pre><code>'123&lt;/a&gt;136&lt;/a&gt;14'
  </code></pre>
  
  <p>This is because the regex is unable to handle nested expression.</p>