Match First Sequence in Regular Expression?

Roger L. Cauvin roger at
Thu Jan 26 18:11:32 CET 2006

"Christos Georgiou" <tzot at> wrote in message 
news:ievht1hkbbbb1hqpu6r1rb5d54klkol1vo at
> On Thu, 26 Jan 2006 16:41:08 GMT, rumours say that "Roger L. Cauvin"
> <roger at> might have written:
>>Good suggestion.  Here are some "test cases":
>>"xyz123aaabbab" accept
>>"xyz123aabbaab" reject
>>"xayz123aaabab" accept
>>"xaaayz123abab" reject
>>"xaaayz123aaabab" accept
> Applying my last regex to your test cases:
>>>> r.match("xyz123aaabbab")
> <_sre.SRE_Match object at 0x00B47F60>
>>>> r.match("xyz123aabbaab")
>>>> r.match("xayz123aaabab")
> <_sre.SRE_Match object at 0x00B50020>
>>>> r.match("xaaayz123abab")
>>>> r.match("xaaayz123aaabab")
> <_sre.SRE_Match object at 0x00B47F60>
>>>> print r.pattern
> ^(?:.*?[^a])?(a{3})(?:b[ab]*)?$
> You should also remember to check the (match_object).start(1) to verify 
> that
> it matches the "aaa" you want.

Thanks, but the second test case I listed contained a typo.  It should have 
contained a sequence of three of the letter 'a'.  The test cases should be:

"xyz123aaabbab" accept
"xyz123aabbaaab" reject
"xayz123aaabab" accept
"xaaayz123abab" reject
"xaaayz123aaabab" accept

Your pattern fails the second test.

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

More information about the Python-list mailing list