How can a recursive regexp be implemented in python?

I'm interested how can be implemented recursive regexp matching in Python (I've not found any examples :( ). For example how would one write expression which matches "bracket balanced" string like "foo(bar(bar(foo)))(foo1)bar1"


You could use pyparsing

#!/usr/bin/env python
from pyparsing import nestedExpr
import sys
astring=sys.argv[1]
if not astring.startswith('('):
    astring='('+astring+')'

expr = nestedExpr('(', ')')
result=expr.parseString(astring).asList()[0]
print(result)

Running it yields:

% test.py "foo(bar(bar(foo)))(foo1)bar1"
['foo', ['bar', ['bar', ['foo']]], ['foo1'], 'bar1']

This is an old question, but for the people who come here through searches:

There's an alternative regex module for python that does support recursive patterns: https://pypi.python.org/pypi/regex

And it has a lot of more nice improvements on re.


You can't do it with a regexp. Python doesn't support recursive regexp