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 "xyz123aaabbaaabab" where you have "aaab" in there twice. -tkc import re tests = [ ("xyz123aaabbab",True), ("xyz123aabbaaab", False), ("xayz123aaabab",True), ("xaaayz123abab", False), ("xaaayz123aaabab",True) ] 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 else: result = False if result == expectedResult: print "%s passed" % test else: print "%s failed (expected %s, got %s)" % ( test,expectedResult,result) -- -- -- http://mail.python.org/mailman/listinfo/python-list