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

M

me

<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.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+\
(([^)]*\)){num_of_open_parenths-1}'\
)
#====================================================================
#HELP!!!!!! I'm not sure how to incorporate:
#1 'match_upto_1st_closed_parenth' 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top