我正在使用pyparsing解析日志文件。我想得到包含关键字的整行:
Input/Output errorDisk fullQuota Exceeded我的代码如下:
import pyparsing as pp
line = pp.Combine(pp.Regex(".*") + pp.CaselessLiteral("Input/Output error") ^ \
pp.CaselessLiteral("Disk full") ^ pp.CaselessLiteral("Quota Exceed") + \
pp.Regex(".*"))在匹配结果中,它只包含关键字,如"Disk full"。整条线都没搞清楚。
有人知道如何在结果中得到全线吗?
发布于 2019-11-11 14:00:06
尝试将此解析操作附加到line
def return_containing_line(s, l, t):
# use pyparsing line builtin to extract the current line text
# at location 'l' in input string 's'
return pp.line(l, s)
line.addParseAction(return_containing_line)这个解析器真的工作吗?我认为前导Regex将使用整行,因为它不对以下文字表达式进行任何前瞻性操作。为什么不放弃Regex术语,只使用searchString呢?或者,如果您使用scanString,您将为每个匹配获得一个(tokens, startloc, endloc)元组,您只需使用startloc和输入字符串调用pp.line,甚至不需要解析操作。
https://stackoverflow.com/questions/58800320
复制相似问题