parenthesis

Lee Harr missive at frontiernet.net
Mon Nov 4 20:42:16 EST 2002


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





More information about the Python-list mailing list