Match First Sequence in Regular Expression?

Tim Chase python.list at
Thu Jan 26 18:47:06 CET 2006

The below seems to pass all the tests you threw at it (taking the 
modified 2nd test into consideration)

One other test that occurs to me would be


where you have "aaab" in there twice.


import re
tests = [
("xyz123aabbaaab", False),
("xaaayz123abab", False),

exp = '^([^b]|((?<!a)b))*aaab+[ab]*$'
r = re.compile(exp)
print "Using regexp: %s" % exp
for test,expectedResult in tests:
     if r.match(test):
         result = True
         result = False
     if result == expectedResult:
         print "%s passed" % test
         print "%s failed (expected %s, got %s)" % (


More information about the Python-list mailing list