Regex for nested {}



hello,
I have a problem matching nested levels of {}, example:

>>> import re
>>> text = """
	outer {
		inner1 { ... }
		inner2 { ... }
	}
	simple { ... }
"""


>>> r = re.compile(r""" ( # OPTION1 .*? # begin \{ # starting { (?: \{.*?\} )* # inner groups \} # ending } )| # OR ( # OPTION2 .*? { .*? } # simple group may also happen ) """, re.DOTALL|re.IGNORECASE|re.UNICODE|re.VERBOSE )


>>> r.findall(text)

>>> [('', '\n\touter { \n\t\tinner1 { ... }'), ('', ' \n\t\tinner2 { .... }'), ('', '
\n\t}\n\tsimple { ... }')]



the regex I currently use stops at the first closing } but what I am looking for is a result as:


[
"outer {
		inner1 { ... }
		inner2 { ... }
	}",
"simple { ... }"
]

is something like that possible?

thanks for any hint
chris
.



Relevant Pages

  • Re: Replace all but the last one.
    ... Curtis wrote: ... But how so I reverse that to replace all full stops except for the ... The reason the regex didn't work for me was because I'm on win32, and the line terminators were CRLFs, which the /m modifier won't recognize. ...
    (comp.lang.php)
  • Re: PHP4 : Extract text from HTML file
    ... How can I change the expression so that it stops at the first occurence ... The cause of the problem is that the regex is greedy (i.e., ... The modifiers in this regex specify that it should be non-greedy, ...
    (comp.lang.php)