Example .tex source with nested commands.
\frac{1}{1+\frac{1}{1+\frac{1}{1+x}}}
In this case, it's impossible to search the othor of third bra's pair with RE.
Let's try!
a = r"\frac{1}{1+\frac{1}{1+\frac{1}{1+x}}}"
m = re.search(r"\\frac\{.*\}\{.*\}", a)
This match to first frac's braket.
'\\frac{1}{1+\\frac{1}{1+\\frac{1}{1+x}}}'
a = r"\frac{1}{1+\frac{1}{1+\frac{1}{1+x}}}"
m =re.search( r"\\frac\{.*?\}\{.*?\}", a)
m.group()
This match to
'\\frac{1}{1+\\frac{1}'
Of cource you can maybe solve this problem using more complicated RE. However, everone hates to write intricately. I think that how to solve it is only parsing strings par one letter. Do you have any othor solusions?