使用pyparsing跳到文本中的第一个可能性

2024-09-28 20:39:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyparsing并尝试使用Skipto方法来获得文本中几个可能的文本的第一次出现。在

想象一下类似的情况:

OneOrMore(SkipTo(...longer expression...) | SkipTo(...another long expression...))

我不能融合这两个skipot,因为它们位于不同的类中,并且它不适合当前的系统来融合这些类。在

如果我现在有一个类似的文本:

^{pr2}$

它只找到Example1事件,而忽略另一个。 现在我的问题是如何跳到文件中的第一个可能性,从而找到所有发生的情况。在


Tags: 方法文本系统another情况pyparsinglongexpression
1条回答
网友
1楼 · 发布于 2024-09-28 20:39:09

如果只尝试处理较大文本体中的位和段,请尝试使用searchString或scanString而不是parseString。在

from pyparsing import oneOf, lineno

sample = """
<<Lot of stuff>>
Example2
<<More stuff>>
Example1
<<Stuff>>"""

expr = oneOf("Example1 Example2")

for toks, start, end in expr.scanString(sample):
    print toks
    print "starts at line", lineno(start, sample)
    print "ends at line", lineno(end, sample)
    print

印刷品

^{pr2}$

相关问题 更多 >