Match First Sequence in Regular Expression?

Roger L. Cauvin roger at deadspam.com
Thu Jan 26 16:53:43 EST 2006


"Peter Hansen" <peter at engcorp.com> wrote in message 
news:mailman.1106.1138308336.27775.python-list at python.org...
> Roger L. Cauvin wrote:
>> "Michael Spencer" <mahs at telcopartners.com> wrote in message 
>> news:mailman.1096.1138299292.27775.python-list at python.org...
>>
>>>Roger L. Cauvin wrote:
>>>>>>"xyz123aaabbab" accept
>>>>>>"xyz123aabbaaab" reject
>>>>>>"xayz123aaabab" accept
>>>>>>"xaaayz123abab" reject
>>>>>>"xaaayz123aaabab" accept
>>>>>>
>>>
>>>This passes your tests.  I haven't closely followed the thread for other 
>>>requirements:
>>>
>>> >>> pattern = ".*?(?<![a+b])aaab" #look for aaab not preceded by any a+b
>>
>> Very interesting.  I think you may have solved the problem.  The key 
>> seems to be the "not preceded by" part.  I'm unfamiliar with some of the 
>> notation. Can you explain what "[a+b]" and the "(?<!" do?
>
> I think you might need to add a test case involving a pattern of aaaab 
> prior to another aaab.  From what I gather (not reading too closely), you 
> would want this to be rejected.  Is that true?
>
> xyz123aaaababaaabab

Adding that test would be a good idea.  You're right; I would want that 
string to be rejected, since in that string the first sequence of 'a' 
directly preceding a 'b' is of length 4 instead of 3.

Thanks for the solution!

-- 
Roger L. Cauvin
nospam_roger at cauvin.org (omit the "nospam_" part)
Cauvin, Inc.
Product Management / Market Research
http://www.cauvin-inc.com





More information about the Python-list mailing list