1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #! /usr/bin/env python
- ## This script tests the raw version of given regular expression on the given input string.
- ##
- ## Version: 1.1 (25-Sep-2012)
- ##
- import sys, optparse, re
- ##======================================================================
- ## main
- def main(argv=None):
- if argv is None:
- arv = sys.argv
- parser = optparse.OptionParser(usage="%prog <REGEX>" +
- "\nTests the raw version of given regular expression on the given input string.", version="%prog 1.1")
-
- parser.add_option("-i", help="ignore case", dest="ignoreCase", action="store_true")
- parser.add_option("--ure", help="decode REGEX to unicode", dest="unicodeRE", action="store_true")
- parser.add_option("--uin", help="decode input to unicode", dest="unicodeIn", action="store_true")
- parser.add_option("--umatch", help="use unicode match", dest="unicodeMatch", action="store_true")
-
- (opts, posArgs) = parser.parse_args()
-
- if len(posArgs) < 1:
- parser.error("At least 1 arguments is required")
-
- if opts.unicodeRE:
- vRE = posArgs[0].decode("UTF-8")
- else:
- vRE = posArgs[0]
-
- # processing input
- vLineCntr = 0
- vMatchCntr = 0
- while True:
- vInput = sys.stdin.readline()
-
- if not vInput:
- break
-
- if opts.unicodeIn:
- vInput = vInput.decode("utf-8")
-
- if opts.ignoreCase:
- if opts.unicodeMatch:
- vPattern = re.compile(vRE, re.IGNORECASE | re.UNICODE)
- else:
- vPattern = re.compile(vRE, re.IGNORECASE)
- else:
- if opts.unicodeMatch:
- vPattern = re.compile(vRE, re.UNICODE)
- else:
- vPattern = re.compile(vRE)
-
- vOutput = re.findall(vPattern, vInput)
- vMatchCntr += len(vOutput)
- if vOutput:
- if opts.unicodeIn:
- vInput = vInput.encode("utf-8")
-
- print vInput,
- print vOutput
- vLineCntr += 1
-
- print "%d matches in %d lines" % (vMatchCntr, vLineCntr)
-
- ##======================================================================
- ## calling main
- if __name__ == "__main__":
- sys.exit(main())
|