tags:

views:

100

answers:

1

While searching for a solution to a python regular expression problem I found this page which demonstrates that [some version of] perl allows variables within regular expressions.

e.g. a perl regex something like:

^(?{ local $d=0}\((?{ $d++ }.*?\)(?d--)

Where variable $d is incremented and decremented depending on which part of the regex you're in. (See the link for a real example)

Is there something equivalent in python?

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 nine nines of uptime.

+2  A: 

No. You need a grammer - pyparsing is nice (and easy)

THC4k
sad. I was hoping to not install another lib.
Ross Rogers
At least it's only one... And a good one at that!
jathanism