In article <2259b0e2.0211041224.7c24635b at posting.google.com>, Michele Simionato wrote: > Suppose I want to parse the following expression: > >>>> exp='(a*(b+c*(2-x))+d)+f(s1)' > > I want to extract the first part, i.e. '(a*(b+c*(2-x))+d)'. > n = 0 for c in range(len(exp)): l = exp[c] if l == '(': n += 1 elif l == ')': n -= 1 if not n: out = exp[0:c+1] break print out