Match First Sequence in Regular Expression?

Tim Chase python.list at tim.thechases.com
Thu Jan 26 12:47:06 EST 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

	"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)








-- 
--




More information about the Python-list mailing list