Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 395
giolekva Avatar asked Nov 01 '09 10:11

giolekva


People also ask

Does Python support recursive regex?

Now, it's not possible to do it with the built-in re package in Python because it doesn't support recursive pattern! To solve this problem with a regular expression in Python then, you need to install the regex package which is more compatible with PCRE.

Can regex be recursive?

The regex must match at least one character before the next recursion, so that it will actually advance through the string. The regex also needs at least one alternative that does not recurse, or the recursion itself must be optional, to allow the recursion to stop at some point.

Can you do regex in Python?

Python, Java, and Perl all support regex functionality, as do most Unix tools and many text editors.


1 Answers

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']
like image 160
unutbu Avatar answered Sep 19 '22 19:09

unutbu