creating a pattern using a previous match and a count of the number of '('s in it

me mgbg25171 at
Tue Jan 27 13:02:23 CET 2009

I'm new to regexs and trying to get a list of all my C++ methods with balanced 
parenthesis as follows.

#find all c++ method prototypes with a '::' in the middle 
#upto and including the 1st closing parenthesis
pattern_upto_1st_closed_parenth  = re.compile('\w+::\w+\([^)]*\)') 
match_upto_1st_closed_parenth    = 
num_of_protos = len(match_upto_1st_closed_parenth)

for i in range (0,num_of_protos-1):                  
   num_of_open_parenths = match_upto_1st_closed_parenth[i].count('(')
   #expand the pattern to get all of the prototype 
   #ie upto the last closed parenthesis
   #saying something like
   pattern = re.compile(\
   #HELP!!!!!! I'm not sure how to incorporate:
   #1 'match_upto_1st_closed_parenth[i]' into the above extended pattern???
   #2 the count 'num_of_open_parenths' instead of a literal ???

#if I could do it this sort of this would appear to offer the neatest solution
pattern_upto_last_balanced_parenthesis  = re.compile('

Should I be using regexs to do this?

I've only put \ line extensions to separate the pattern components to assist 


More information about the Python-list mailing list