regex (?!..) problem

Wolfgang Rohdewald wolfgang at
Mon Oct 5 06:34:30 CEST 2009


I want to match a string only if a word (C1 in this example) appears
at most once in it. This is what I tried:

>>> re.match(r'(.*?C1)((?!.*C1))','C1b1b1b1 b3b3b3b3 C1C2C3').groups()
('C1b1b1b1 b3b3b3b3 C1', '')
>>> re.match(r'(.*?C1)','C1b1b1b1 b3b3b3b3 C1C2C3').groups()

but this should not have matched. Why is the .*? behaving greedy
if followed by (?!.*C1)? I would have expected that re first 
evaluates (.*?C1) before proceeding at all.

I also tried:

>>>'(.*?C1(?!.*C1))','C1b1b1b1 b3b3b3b3 
('C1b1b1b1 b3b3b3b3 C1',)

with the same problem.

How could this be done?


More information about the Python-list mailing list