首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过pyparsing解析获得包含多个关键字的整行?

如何通过pyparsing解析获得包含多个关键字的整行?
EN

Stack Overflow用户
提问于 2019-11-11 11:19:21
回答 1查看 147关注 0票数 2

我正在使用pyparsing解析日志文件。我想得到包含关键字的整行:

  • Input/Output error
  • Disk full
  • Quota Exceeded

我的代码如下:

代码语言:javascript
运行
复制
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"。整条线都没搞清楚。

有人知道如何在结果中得到全线吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-11 14:00:06

尝试将此解析操作附加到line

代码语言:javascript
运行
复制
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,甚至不需要解析操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58800320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档