Wolfgang Rohdewald wrote:
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'

"(?!.*?(C1).*?\1)" will succeed only if ".*?(C1).*?\1" has failed, in
which case the group (group 1) will be undefined (no capture).
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to