Matching Nested Structures With Regular Expressions in Python

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 … Read more

Recursive pattern in regex

The pattern is: {((?>[^{}]+|(?R))*)} You can see this works for your example: regex.findall(“{((?>[^{}]+|(?R))*)}”, “{1, {2, 3}} {4, 5}”) # [‘1, {2, 3}’, ‘4, 5’] Explanation: The m part needs to exclude the brackets. The use of an atomic group is needed if you want at the same time to allow a quantifier for [^{}] and … Read more