regex question
Tim Chase
python.list at tim.thechases.com
Thu Aug 3 21:08:27 EDT 2006
> That's great! Thanks for the quick response. Yeah, abcdcd should be
> possible too.
The below passes that test now as well as a couple others I
tossed at it.
I changed it from a one-line regexp to a VERBOSE regexp to make
it easier to read and see what's going on. You may be able to
see the pattern building there, so if you need to add additional
stages/letters, it should likely follow the same pattern.
-tkc
import re
tests = [
('aabbbaabbcccbbbcccddd', True),
('aabcabcd', True),
('abcd', True),
('aaaaabbbbbccccaaaaadddd', False),
('aaaaaaaaaaabbbbbccc', False),
('abcdcd', True),
('abccccdaaaabbbbccccd', True),
('abccccdccccd', True),
]
#regex = r'^(a+b+)+(c+(a*b+c+)*)d+$'
regex = r"""
^
a+
b+(a*b+)*
c+((a*b+)*c+)*
d+(((a*b+)*c+)*d+)*
$"""
r = re.compile(regex, re.VERBOSE)
for test, expected in tests:
matched = (r.match(test) is not None)
if matched == expected:
print "PASSED: %s with %s" % (test, expected)
else:
print "FAILED: %s with %s" % (test, expected)
More information about the Python-list
mailing list