#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Find a regex accepting any arbitrary string but "01234". (C) 2015, Public Domain, meisterluk """ import re import sys # The following regexes are valid solutions: # ^(.|..|...|....|[^0]....|.[^1]...|..[^2]..|...[^3].|....[^4]|......+)$ # ^(?!01234$).*$ # ^(?!01234$)(.|\n)*$ PATTERN = re.compile('^(|.|..|...|....|[^0]....|.[^1]...|..[^2]..|...[^3].|....[^4]|......+)$') testsuite = { '' : True, '0' : True, '00' : True, 'A' : True, 'ABCD' : True, '01' : True, '012' : True, '01234' : False, '012340' : True, '012345' : True, '0123456' : True, '0123401234' : True } def main(pat): """Main routine""" for string, match in testsuite.items(): if bool(pat.search(string)) == match: print('PASS ', end='') else: print('FAIL ', end='') print('for {0} (shall be {1})'.format(string.ljust(11), match)) if __name__ == '__main__': if len(sys.argv) > 1: pat = re.compile(sys.argv[1], flags=re.S | re.U) else: pat = PATTERN sys.exit(main(pat))