令牌捕获序列python无法正常工作 [英] token capture sequences python not working correctly
问题描述
我正在测试我的 python grpper 脚本.它需要多种搜索模式,并且适用于正则表达式和纯文本.
I am testing out my python grepper script. It takes multiple search patterns, and works well with regular expressions and plain text.
对于这个测试,我的 input.txt 有以下两行:
For this test, my input.txt has the following two lines:
foo blah is a bar
foo blah is bar
如果我跑:
cat input.txt | ./pgreper.py "foo %{0} is a %{1}"
我应该得到:foo blah 是一个 bar,作为输出,但是我什么也没得到.
I should get: foo blah is a bar, as output, however I get nothing.
在这方面的任何帮助将不胜感激.
Any help in this area would be greatly appreciated.
谢谢:)
ps 请忽略我的评论.
ps please ignore my comments.
#!/usr/bin/python3
import sys
import re
import time
import datetime
import inspect
import argparse
# The following section allows arguments to be passed to the module, eg input | ./pgreper.py pattern pattern --debug
parser = argparse.ArgumentParser(description='Python Grep.')
# Enable debugging, by adding --debug at the end of command
parser.add_argument('--debug', action='store_true', help='Print debug messages')
# nargs='+', enables multiple patterns to be entered at the commandline eg input | ./pgreper.py pattern pattern
parser.add_argument('pattern', type=str, nargs='+', help='Pattern(s) for pgrepping')
args = parser.parse_args()
# This is the class that allows for debugging a line in sys.stdin, if it matches all patterns.
class CodeTrace(object):
def __init__(self, line, pattern):
self.line = line
self.pattern = pattern
# @staticmethod
# This is the degugging method
def trace(self, line, pattern):
# Capture the current time, and format the timestamp, into a readable format
ts = datetime.datetime.fromtimestamp(time.time()).strftime('[%Y-%m-%d %H:%M:%S:%f]')
# Inspect allows us to blah, inspecting the stack allows us to retrieve information
stack = inspect.stack()
# Retrieve calling class information
the_class = stack[1][0].f_locals["self"].__class__
# Retrieves the calling method information
the_method = stack[1][0].f_code.co_name
# Retrieves the calling method's variables
the_variables = stack[1][0].f_code.co_varnames
# Formats the contents of the debug trace into a readable format,
# Any parameters passed to the method and the return value, are included in the debug trace
debug_trace = ("{} {}.{}.{} {} {} ".format(ts, str(the_class), the_method,the_variables, pattern, line))
# Send out the debug trace as a standard error output
sys.stderr.write(debug_trace + "\n")
# This is the class that does the pattern matching
class Grepper(object):
def __init__(self, patterns, debug=False):
# Every pattern that this module takes is compiled here, so that it may be searched for in sys.stdin
self.patterns = [re.compile(p) for p in patterns]
self.debug = debug
# This method compares the input, to the patterns compiled in Grepper
def matchline(self, debug):
for line in sys.stdin:
# This line compares all the patterns to the input, only if the input matches ALL patterns does it pass
if all(p.search(line) for p in self.patterns):
sys.stdout.write(line)
# this if statement runs the CodeTrace.trace function, if the user adds the --debug option in the cli
if self.debug:
CodeTrace(line, self.patterns).trace(line, args.pattern)
# This main function calls the grepper class and the matchline method, for the purpose of pattern matching
def main():
print(args.pattern)
Grepper(args.pattern, args.debug).matchline(args.debug)
# This allows the module to be used as a standalone module, or a reusable module in a different program
if __name__ == "__main__":
main()
推荐答案
您当前将命令行参数解释为正则表达式模式,而不替换占位符 %{0}
和 %{1}
.您可能想用 .*
或类似的东西替换占位符,如下所示:
You are currently interpreting the command line arguments as regex patterns, without replacing the placeholders %{0}
and %{1}
. You'll probably want to replace the placeholders with .*
or something similar, like so:
class Grepper(object):
def __init__(self, patterns, debug=False):
patterns= [re.sub(r'%\{\d\}', '.*', p) for p in patterns]
self.patterns = [re.compile(p) for p in patterns]
这篇关于令牌捕获序列python无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!