No Description

regex-tester.py 801B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import re
  2. import sys
  3. # EDIT THIS REGEXP
  4. REGEXP="REGEXP"
  5. # DO NOT CHANGE ANYTHING BELOW THIS LINE
  6. def validate(value, regexp):
  7. """
  8. Function to validate a string stored in variable value given
  9. a regular expression in regexp.
  10. """
  11. val = re.compile(regexp)
  12. result = val.match(value)
  13. if result and value == result.group(0):
  14. return True
  15. return False
  16. def main():
  17. try:
  18. fd = open(sys.argv[1])
  19. lines = fd.readlines()
  20. except:
  21. print "Could not open strings file."
  22. print "Usage: python %s <strings_file>" % sys.argv[0]
  23. sys.exit(0)
  24. passed = 0
  25. failed = 0
  26. for line in lines:
  27. line = line.strip()
  28. if validate(line, REGEXP):
  29. print line, "PASS"
  30. passed+=1
  31. else:
  32. print line, "FAIL"
  33. failed+=1
  34. print "%s strings passed, %s strings failed" % (passed, failed)
  35. main()