title: Does python have the equivalent of Perl's regex "local" variable?
body: <p>While searching for a solution to a python regular expression problem I found <a href="http://perl.plover.com/yak/regex/samples/slide083.html" rel="nofollow noreferrer">this page</a> which demonstrates that [some version of] perl allows variables within regular expressions. </p>
  
  <p>e.g. a perl regex <em>something</em> like:</p>
  
  <pre><code>^(?{ local $d=0}\((?{ $d++ }.*?\)(?d--)
  </code></pre>
  
  <p>Where variable <code>$d</code> is incremented and decremented depending on which part of the regex you're in.
  <a href="http://perl.plover.com/yak/regex/samples/slide083.html" rel="nofollow noreferrer">(See the link for a real example)</a></p>
  
  <p>Is there something equivalent in python?</p>
  
  <p>Note: I realize that you can achieve this goal with a real parser and a grammar, but I don't want to add another dependency to overcome this tiny problem for a script I'm writing.  And no, this script doesn't have to have <a href="http://en.wikipedia.org/wiki/Uptime" rel="nofollow noreferrer">nine nines of uptime</a>.</p>