Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching Nested Structures With Regular Expressions in Python

I seem to remember that Regular Expressions in DotNet have a special mechanism that allows for the correct matching of nested structures, like the grouping in "( (a ( ( c ) b ) ) ( d ) e )".

What is the python equivalent of this feature? Can this be achieved using regular expressions with some workaround? (Though it seems to be the sort of problem that current implementations of regex aren't designed for)

like image 731
Victor Yan Avatar asked Jul 08 '09 16:07

Victor Yan


1 Answers

Regular expressions cannot parse nested structures. Nested structures are not regular, by definition. They cannot be constructed by a regular grammar, and they cannot be parsed by a finite state automaton (a regular expression can be seen as a shorthand notation for an FSA).

Today's "regex" engines sometimes support some limited "nesting" constructs, but from a technical standpoint, they shouldn't be called "regular" anymore.

like image 93
Svante Avatar answered Oct 21 '22 17:10

Svante