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

Discussion in 'Python' started by me, Jan 27, 2009.

  1. me

    me Guest

    <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>
     
    me, Jan 27, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    3
    Views:
    1,028
    David Carlisle
    Feb 16, 2005
  2. cirfu
    Replies:
    1
    Views:
    239
    Carl Banks
    Jun 23, 2008
  3. efelnavarro09
    Replies:
    2
    Views:
    977
    efelnavarro09
    Jan 26, 2011
  4. j  ellings

    RegEx - matching previous match

    j ellings, Feb 27, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    225
    j ellings
    Feb 28, 2008
  5. j  ellings

    RegEx - matching previous match

    j ellings, Feb 27, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    103
    j ellings
    Feb 27, 2008
Loading...

Share This Page