regular expression for nested parentheses
John Machin
sjmachin at lexicon.net
Sun Dec 9 16:41:17 EST 2007
More information about the Python-list mailing list
Sun Dec 9 16:41:17 EST 2007
- Previous message (by thread): regular expression for nested parentheses
- Next message (by thread): regular expression for nested parentheses
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Dec 10, 8:13 am, Noah Hoffman <noah.hoff... at gmail.com> wrote: > I have been trying to write a regular expression that identifies a > block of text enclosed by (potentially nested) parentheses. I've found > solutions using other regular expression engines (for example, my text > editor, BBEdit, which uses the PCRE library), but have not been able > to replicate it using python's re module. A pattern that can validly be described as a "regular expression" cannot count and thus can't match balanced parentheses. Some "RE" engines provide a method of tagging a sub-pattern so that a match must include balanced () (or [] or {}); Python's doesn't. Looks like you need a parser; try pyparsing. [snip] > py> rexp = r"""(?P<parens> > ... \( > ... (?> > ... (?> [^()]+ ) | > ... (?P>parens) > ... )* > ... \) > ... )""" > py> print re.findall(no_ws(rexp), text) Ummm ... even if Python's re engine did do what you want, wouldn't you need flags=re.VERBOSE in there?
- Previous message (by thread): regular expression for nested parentheses
- Next message (by thread): regular expression for nested parentheses
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list