Match First Sequence in Regular Expression?

Scott David Daniels scott.daniels at acm.org
Fri Jan 27 16:17:05 EST 2006


How about:
pattern = re.compile('^([^a]|(a+[^ab]))*aaab')

Which basically says, "precede with arbitrarily many non-a's
or a sequences ending in non-b, then must have 3 as followed by a b."

cases = ["xyz123aaabbab", "xayz123aaabab", "xaaayz123aaabab",
	"xyz123aaaababaaabab", "xyz123aabbaaab", "xaaayz123abab"]
[re.search(pattern, case) is not None for case in cases]
[True, True, True, False, False, False]

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list