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

me mgbg25171 at blueyonder.co.uk
Tue Jan 27 13:02:23 CET 2009


<code>
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    = 
re.findall(pattern_upto_1st_closed_parenth,txt)
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(\
                        'match_upto_1st_closed_parenth[i]+\ 
                        (([^)]*\)){num_of_open_parenths-1}'\
                       )
   #====================================================================
   #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('
									(\w+::\w+\([^)]*\))\			
                                 					([^)]*\)){\1.count('(')-1}		
                                                              		') 		
#=======================================

Should I be using regexs to do this?

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

Thx
</code>




More information about the Python-list mailing list