Match First Sequence in Regular Expression?
Michael Spencer
mahs at telcopartners.com
Thu Jan 26 13:10:51 EST 2006
Roger L. Cauvin wrote:
> "Fredrik Lundh" <fredrik at pythonware.com> wrote in message
> news:mailman.1088.1138296051.27775.python-list at python.org...
>> Roger L. Cauvin wrote:
>>
>>>> $ python test.py
>>>> got expected
>>>> ---------------
>>>> accept accept
>>>> reject reject
>>>> accept accept
>>>> reject reject
>>>> accept accept
>>> 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
>>>
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
>>> test(pattern)
got expected
------ --------
accept accept
reject reject
accept accept
reject reject
accept accept
>>>
>>> testsuite = (
... ("xyz123aaabbab", "accept"),
... ("xyz123aabbaaab", "reject"),
... ("xayz123aaabab", "accept"),
... ("xaaayz123abab", "reject"),
... ("xaaayz123aaabab", "accept"),
... )
>>>
>>> def test(pattern):
... print "got expected"
... print "------ --------"
... for string, result in testsuite:
... m = re.match(pattern, string)
... if m:
... print "accept",
... else:
... print "reject",
... print result
...
>>>
Michael
More information about the Python-list
mailing list