Regex for nested {}
- From: Chris <c@xxxxxxx>
- Date: Thu, 28 Jul 2005 18:17:30 +0200
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 .
- Follow-Ups:
- Re: Regex for nested {}
- From: skip
- Re: Regex for nested {}
- From: Diez B . Roggisch
- Re: Regex for nested {}
- Prev by Date: Re: baffling error-handling problem
- Next by Date: Re: Rich Graphics?
- Previous by thread: codecs.getencoder encodes entire string ?
- Next by thread: Re: Regex for nested {}
- Index(es):
Relevant Pages
|