Skip to content

Instantly share code, notes, and snippets.

@havron
Last active May 15, 2021 00:55
Show Gist options
  • Save havron/ed1026df7bbc84a00b5f3db61d241e44 to your computer and use it in GitHub Desktop.
Save havron/ed1026df7bbc84a00b5f3db61d241e44 to your computer and use it in GitHub Desktop.
Check the regexes of a file split by '"validation": '
import re
def sanitize(regex):
if regex.startswith('r"'):
regex = regex[2:]
elif regex.startswith('"'):
regex = regex[1:]
if regex.endswith('"'):
regex = regex[:-1]
return regex
def compiles(regex):
if 'EMAIL_RE' in regex:
return True
try:
return bool(re.compile(regex))
except re.error:
return False
return False
if __name__ == '__main__':
with open('validations.txt', 'r') as f:
lines = [line.strip(' ,\n').split('"validation": ') for line in f]
for line in lines:
regex = sanitize(line[1])
if not compiles(regex):
print(line[0], regex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment