I would use zip instead of regular expressions. It aligns all elements of both lines and allows you to scroll through each pair.
def verify(pat, inp): for n,h in zip(pat, inp): if n == '*': if h not in ('0', '1'): return False elif h not in ('0', '1'): return False elif n != h: return False return True
# Example use: >>> verify('**1', '001') True >>> verify('**1', '101') True >>> verify('**1', '000') False
A shorter way to do this with @DSM.
def verify(n, h): return all(c0 == c1 or (c0 == '*' and c1 in '01') for c0, c1 in zip(n, h))
source share