Python re repetative matching

Rich crawley.storm at IveGotEnoughSpamAsItIS.com
Mon Dec 22 18:09:27 EST 2003


Im new to regex's and cant quite figure out how to get them to work, what 
I want is a tuple of all the matches from the regex.  Ive simplified my 
actual problem and still cant get it to work

Ive so far got this:
    	
print re.findall( r'(@\d+)|(\w+)', "@5489 heel all and thumb toe" )

This dose exactly what I want, except it matches both matches each time, 
so I end up with a list full tuples each with blank elements.... so close

I also tried my orginal idea

a = re.match( r'(@\d+)\s+(\w+)', "@5489 heel all and thumb toe" )
print a.groups()

This matches the number and the first word, so I thought the following 
should rematch after the first word and give me what I wanted... but it 
dosent for some reason

a = re.match( r'(@\d+)\s+(?:(\w+)\s*)', "@5489 heel all and thumb toe" )
print a.groups()

This is my next iteration, still gives me the number (first group) and 
only the word (the second match).  So I extend it to ...

a = re.match( r'(@\d+)\s+(?:(\w+)\s*)*', "@5489 heel all and thumb toe" )
print a.groups()

Now this gives me the number and the last but one word ? WHY!

My logic suggests that this should do what I want... what am I missing, 
Ive spent all night trying to figure this out.  

Cheers

Rich




More information about the Python-list mailing list