Match First Sequence in Regular Expression?

Roger L. Cauvin roger at
Thu Jan 26 11:23:05 EST 2006

"Tim Chase" <python.list at> wrote in message 
news:mailman.1082.1138290258.27775.python-list at
>> Sorry for the confusion.  The correct pattern should reject
>> all strings except those in which the first sequence of the
>> letter 'a' that is followed by the letter 'b' has a length of
>> exactly three.
> Ah...a little more clear.
> r = re.compile("[^a]*a{3}b+(a+b*)*")
> matches = [s for s in listOfStringsToTest if r.match(s)]

Wow, I like it, but it allows some strings it shouldn't.  For example:


(It skips over the two-letter sequence of 'a' and matches 'bbaaab'.)

Roger L. Cauvin
nospam_roger at (omit the "nospam_" part)
Cauvin, Inc.
Product Management / Market Research

More information about the Python-list mailing list