Regexp and multiple groups (with repeats)

Discussion in 'Python' started by mk, Nov 20, 2009.

  1. mk

    mk Guest

    Hello,

    >>> r=re.compile(r'(?:[a-zA-Z]:)([\\/]\w+)+')


    >>> r.search(r'c:/tmp/spam/eggs').groups()

    ('/eggs',)

    Obviously, I would like to capture all groups:
    ('/tmp', '/spam', '/eggs')

    But it seems that re captures only the last group. Is there any way to
    capture all groups with repeat following it, i.e. (...)+ or (...)* ?

    Even better would be:

    ('tmp', 'spam', 'eggs')

    Yes, I know about re.split:

    >>> re.split( r'(?:\w:)?[/\\]', r'c:/tmp/spam\\eggs/' )

    ['', 'tmp', 'spam', '', 'eggs', '']

    My interest is more general in this case: how to capture many groups
    with a repeat?

    Regards,
    mk
    mk, Nov 20, 2009
    #1
    1. Advertising

  2. mk

    Neil Cerutti Guest

    On 2009-11-20, mk <> wrote:
    > Hello,
    >
    > >>> r=re.compile(r'(?:[a-zA-Z]:)([\\/]\w+)+')

    >
    > >>> r.search(r'c:/tmp/spam/eggs').groups()

    > ('/eggs',)
    >
    > Obviously, I would like to capture all groups:
    > ('/tmp', '/spam', '/eggs')


    You'll have to do something else, for example:

    >>> s = re.compile(r'(?:[a-zA-Z]:)')
    >>> n = re.compile(r'[\\/]\w+')
    >>> m = s.match('c:/tmp/spam/eggs')
    >>> n.findall(m.string[m.end():])

    ['/tmp', '/spam', '/eggs']

    --
    Neil Cerutti
    Neil Cerutti, Nov 20, 2009
    #2
    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. kj12345

    Refresh repeats last action

    kj12345, Sep 28, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    503
    Eliyahu Goldin
    Sep 29, 2004
  2. Mick
    Replies:
    1
    Views:
    706
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
    Nov 29, 2004
  3. Joao Silva
    Replies:
    16
    Views:
    359
    7stud --
    Aug 21, 2009
  4. Replies:
    2
    Views:
    97
    Dr John Stockton
    Sep 25, 2006
  5. cate
    Replies:
    1
    Views:
    206
    Evertjan.
    Jun 14, 2010
Loading...

Share This Page