Regex for nested {}
Chris
c at cdot.de
Thu Jul 28 12:17:30 EDT 2005
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
More information about the Python-list
mailing list