Connecting Tech Pros Worldwide Help | Site Map

how to get all repeated group with regular expression

  #1  
Old November 21st, 2008, 02:35 PM
scsoce
Guest
 
Posts: n/a
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 ?

  #2  
Old November 21st, 2008, 03:05 PM
Hrvoje Niksic
Guest
 
Posts: n/a

re: how to get all repeated group with regular expression


scsoce <scsoce@gmail.comwrites:
Quote:
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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Re: how to get all repeated group with regular expression scsoce answers 0 November 22nd, 2008 02:15 AM
Re: how to get all repeated group with regular expression M.-A. Lemburg answers 0 November 21st, 2008 05:45 PM
Re: how to get all repeated group with regular expression Steve Holden answers 0 November 21st, 2008 04:35 PM
Re: how to get all repeated group with regular expression Steve Holden answers 0 November 21st, 2008 03:05 PM