Match First Sequence in Regular Expression?

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

"Christos Georgiou" <tzot at> wrote in message 
news:boqht19rs7946mtk5s64hqrieq44he5aq7 at
> On Thu, 26 Jan 2006 14:09:54 GMT, rumours say that "Roger L. Cauvin"
> <roger at> might have written:
>>Say I have some string that begins with an arbitrary sequence of 
>>and then alternates repeating the letters 'a' and 'b' any number of times,
>>I'm looking for a regular expression that matches the first, and only the
>>first, sequence of the letter 'a', and only if the length of the sequence 
>>exactly 3.
>>Does such a regular expression exist?  If so, any ideas as to what it 
> Is this what you mean?
> ^[^a]*(a{3})(?:[^a].*)?$

Close, but the pattern should allow "arbitrary sequence of characters" that 
precede the alternating a's and b's to contain the letter 'a'.  In other 
words, the pattern should accept:


since the 'a' between the 'x' and 'y' is not directly followed by a 'b'.

Your proposed pattern  rejects this string.

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

More information about the Python-list mailing list