Match First Sequence in Regular Expression?

Fredrik Lundh fredrik at pythonware.com
Thu Jan 26 12:01:07 EST 2006


Roger L. Cauvin wrote:

> Good suggestion.  Here are some "test cases":
>
> "xyz123aaabbab" accept
> "xyz123aabbaab" reject
> "xayz123aaabab" accept
> "xaaayz123abab" reject
> "xaaayz123aaabab" accept

$ more test.py

import re

print "got    expected"
print "------ --------"

testsuite = (
    ("xyz123aaabbab", "accept"),
    ("xyz123aabbaab", "reject"),
    ("xayz123aaabab", "accept"),
    ("xaaayz123abab", "reject"),
    ("xaaayz123aaabab", "accept"),
    )

for string, result in testsuite:
    m = re.search("aaab", string)
    if m:
        print "accept",
    else:
        print "reject",
    print result


$ python test.py
got    expected
---------------
accept accept
reject reject
accept accept
reject reject
accept accept

</F>






More information about the Python-list mailing list