12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #! /usr/bin/env python
- ## This script substitutes the re.findall() of python.
- ##
- ## 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>" +
- "\nSubstitutes the re.findall() of python.", 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")
- parser.add_option("-p", help="print input", dest="prnInput", 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
- 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.useUnicode:
- vPattern = re.compile(vRE, re.UNICODE)
- else:
- vPattern = re.compile(vRE)
-
- vlOutput = re.findall(vPattern, vInput)
- if vlOutput:
- if opts.unicodeIn:
- vInput = vInput.encode("utf-8")
-
- if opts.prnInput:
- print vInput
-
- for vOutput in vlOutput:
- if opts.unicodeIn:
- vOutput = vOutput.encode("utf-8")
- print vOutput
-
- ##======================================================================
- ## calling main
- if __name__ == "__main__":
- sys.exit(main())
|