how to get all repeated group with regular expression

Discussion in 'Python' started by scsoce, Nov 21, 2008.

  1. scsoce

    scsoce Guest

    say, when I try to search and match every char from variable length
    string, such as string '123456', i tried re.findall( r'(\d)*, '12346' )
    , but only get '6' and Python doc indeed say: "If a group is contained
    in a part of the pattern that matched multiple times, the last match is
    returned."
    cause the regx engine cannot remember all the past history then ? is it
    nature to all regx engine or only to Python ?
     
    scsoce, Nov 21, 2008
    #1
    1. Advertising

  2. scsoce <> writes:

    > say, when I try to search and match every char from variable length
    > string, such as string '123456', i tried re.findall( r'(\d)*, '12346'
    > ) , but only get '6' and Python doc indeed say: "If a group is
    > contained in a part of the pattern that matched multiple times, the
    > last match is returned."


    Well, re.findall(r'(\d)*', '123456') returns ['6', ''] for me, but
    that's because re.findall returns the entire match, regardless of
    group contents. What you probably meant was something like
    re.search(r'(\d)*', '123456').group(1), which indeed returns '6', the
    contents of the last group matched.

    What problem are you trying to solve? Depending on this, the best
    tool might be either findall/finditer, or search/match.
     
    Hrvoje Niksic, Nov 21, 2008
    #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. VSK
    Replies:
    2
    Views:
    2,335
  2. Jay Douglas
    Replies:
    3
    Views:
    3,521
    Jay Douglas
    Aug 27, 2004
  3. Replies:
    7
    Views:
    319
    Larry Bates
    Feb 24, 2006
  4. Robert Dodier
    Replies:
    4
    Views:
    339
    Raymond Hettinger
    Mar 14, 2006
  5. MRAB
    Replies:
    0
    Views:
    400
Loading...

Share This Page