regex (?!..) problem
Wolfgang Rohdewald
wolfgang at rohdewald.de
Mon Oct 5 04:09:19 EDT 2009
On Monday 05 October 2009, MRAB wrote:
> You're currently looking for one that's not followed by another;
> the solution is to check first whether there are two:
>
> >>> re.match(r'(?!.*?C1.*?C1)(.*?C1)','C1b1b1b1 b3b3b3b3
> C1C2C3').groups()
>
> Traceback (most recent call last):
> File "<pyshell#3>", line 1, in <module>
> re.match(r'(?!.*?C1.*?C1)(.*?C1)','C1b1b1b1 b3b3b3b3
> C1C2C3').groups() AttributeError: 'NoneType' object has no
> attribute 'groups'
that is a nice solution!
now to make it more similar to my real world case
where C1 is actually part of the string:
same regex but using a group for C1 does not work - why?
>>> re.match(r'(?!.*?(C1).*?\1)(.*?\1)','C1b1b1b1 b3b3b3b3 C2C2C3').groups()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
>>> re.match(r'(?!.*?(?P<tile>C1).*?(?P=tile))(.*?(?P=tile))','C1B1B1B1 b3b3b3b3 C2C2C3').groups()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
--
Wolfgang
More information about the Python-list
mailing list