re-test.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #! /usr/bin/env python
  2. ## This script tests the raw version of given regular expression on the given input string.
  3. ##
  4. ## Version: 1.1 (25-Sep-2012)
  5. ##
  6. import sys, optparse, re
  7. ##======================================================================
  8. ## main
  9. def main(argv=None):
  10. if argv is None:
  11. arv = sys.argv
  12. parser = optparse.OptionParser(usage="%prog <REGEX>" +
  13. "\nTests the raw version of given regular expression on the given input string.", version="%prog 1.1")
  14. parser.add_option("-i", help="ignore case", dest="ignoreCase", action="store_true")
  15. parser.add_option("--ure", help="decode REGEX to unicode", dest="unicodeRE", action="store_true")
  16. parser.add_option("--uin", help="decode input to unicode", dest="unicodeIn", action="store_true")
  17. parser.add_option("--umatch", help="use unicode match", dest="unicodeMatch", action="store_true")
  18. (opts, posArgs) = parser.parse_args()
  19. if len(posArgs) < 1:
  20. parser.error("At least 1 arguments is required")
  21. if opts.unicodeRE:
  22. vRE = posArgs[0].decode("UTF-8")
  23. else:
  24. vRE = posArgs[0]
  25. # processing input
  26. vLineCntr = 0
  27. vMatchCntr = 0
  28. while True:
  29. vInput = sys.stdin.readline()
  30. if not vInput:
  31. break
  32. if opts.unicodeIn:
  33. vInput = vInput.decode("utf-8")
  34. if opts.ignoreCase:
  35. if opts.unicodeMatch:
  36. vPattern = re.compile(vRE, re.IGNORECASE | re.UNICODE)
  37. else:
  38. vPattern = re.compile(vRE, re.IGNORECASE)
  39. else:
  40. if opts.unicodeMatch:
  41. vPattern = re.compile(vRE, re.UNICODE)
  42. else:
  43. vPattern = re.compile(vRE)
  44. vOutput = re.findall(vPattern, vInput)
  45. vMatchCntr += len(vOutput)
  46. if vOutput:
  47. if opts.unicodeIn:
  48. vInput = vInput.encode("utf-8")
  49. print vInput,
  50. print vOutput
  51. vLineCntr += 1
  52. print "%d matches in %d lines" % (vMatchCntr, vLineCntr)
  53. ##======================================================================
  54. ## calling main
  55. if __name__ == "__main__":
  56. sys.exit(main())