I would like to match various cominations of nested matched parentheses that do not exceed a given depth.
If the depth is two, the following should match
H<>
H<a>
H<<a><b>>
H<a<b>c<d>>
These shoud not:
H<<<>>>
H<<<>><<><>><>>
I thought I would make a function that generates the regex
INNER = r"[^<>]"
OPEN = r"<"
CLOSE = r">"
def parenthesis(depth=2):
if depth == 0:
return INNER
inner = "("
for i in range(depth):
inner += parenthesis(i) + "|"
inner = inner[:-1]+")*?"
return "("+OPEN + inner + CLOSE+")"
this seems to work (though I’m not completely confident) if I do, for example:
re.match("H"+parentthesis(2), "H<<>>")
But really I want to match curley brackets that are not escaped. So:
OPEN = r"(?<!\\)[\{]"
CLOSE = r"(?<!\\)[\}]"
and, something I am unsure of:
INNER = r"[^(?<!\\)[\}\{]]"
This doesn’t seem to work. Any ideas. This is giving me a headache!
Try this:
It means an escaped brace, or a character that isn’t a brace.