Regexp : repeated group identification

candide candide at free.invalid
Wed Dec 14 06:12:07 EST 2011


Consider the following code

# ----------------------------
import re

z=re.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8')
print z.group(0)
print z.group(1)
# ----------------------------

outputting :

----------------------------
Spam4Spam2Spam7Spam8
Spam8
----------------------------

The '(Spam\d)+' regexp is tested against 'Spam4Spam2Spam7Spam8' and the 
regexp matches the string.

Group numbered one within the regex '(Spam\d)+' refers to Spam\d

The fours substrings

Spam4   Spam2   Spam7  and  Spam8

match the group numbered 1.

So I don't understand why z.group(1) gives the last substring (ie Spam8 
as the output shows), why not an another one, Spam4 for example ?



More information about the Python-list mailing list