如何通过pyparsing获取包含几个关键词的整行?
How to get the whole line containing several key words by pyparsing?
我正在使用 pyparsing
来解析日志文件。我想获取包含关键字的整行:
Input/Output error
Disk full
Quota 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"
等关键词。它没有得到整条线。
有谁知道如何在结果中得到整行?
尝试将此解析操作附加到 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
,你不会甚至不需要解析操作。
我正在使用 pyparsing
来解析日志文件。我想获取包含关键字的整行:
Input/Output error
Disk full
Quota 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"
等关键词。它没有得到整条线。
有谁知道如何在结果中得到整行?
尝试将此解析操作附加到 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
,你不会甚至不需要解析操作。