Created
October 18, 2011 13:06
-
-
Save ojii/1295377 to your computer and use it in GitHub Desktop.
multi_hex_color_code_re.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
GOOD = { | |
"#abc": ['abc'], | |
"#abc,#123": ['abc', '123'], | |
"567": ['567'], | |
"abc,123": ['abc', '123'], | |
"abc,#611": ['abc', '611'], | |
"#aaff33": ['aaff33'], | |
"9923cc": ['9923cc'], | |
"155421,#53ffcc": ['155421', '53ffcc'], | |
"#11f,441122,#ffccaa": ['11f', '441122', 'ffccaa'], | |
} | |
BAD = [ | |
"#abcd", | |
"1", | |
"4261", | |
"#abw", | |
"#cc3,#552ffx", | |
"#ffc;333", | |
] | |
def check(pattern): | |
compiled = re.compile(pattern) | |
for good, output in GOOD.items(): | |
match = compiled.match(good) | |
if not match: | |
print "Pattern %r does not match %r but should" % (pattern, good) | |
group = match.group(0) | |
bits = [bit.strip('#') for bit in group.split(',')] | |
if bits != output: | |
print "Pattern %r matches %r as expected, but extracted bits are %r not %r" % (pattern, good, bits, output) | |
for bad in BAD: | |
if compiled.match(bad): | |
print "Pattern %r matched %r but shouldn't" % (pattern, bad) | |
check(r'^(?:#?(?:[0-9a-fA-F]{3}){1,2},?)+$') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment