prog = re.compile('([A-Z]\\d+)+')
while True:
    line = sys.stdin.readline()
if not line:
    break
if prog.match(line):
    print('matched')
else:
    print('not matched')
Check if string matches pattern