开发者

Does python have the equivalent of Perl's regex "local" variable?

开发者 https://www.devze.com 2022-12-17 08:19 出处:网络
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.

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 开发者_Go百科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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消