3

我有一个简单的pyparsing语法,可以匹配用空格分隔的数字:

from pyparsing import *
NUMBER = Word( nums )
STATEMENT = ZeroOrMore( NUMBER )
print( STATEMENT.parseString( "1 2 34" ) )

给定1 2 34测试字符串,它返回 3 个已解析标记的字符串。但是如何在原始字符串中找到每个标记的位置?我需要它来突出显示“某种”语法。

4

1 回答 1

5

将此解析操作添加到 NUMBER:

NUMBER.setParseAction(lambda locn,tokens: (locn,tokens[0]))

解析动作可以传递为给定表达式解析的标记、解析的位置和原始字符串。您可以setParseAction使用以下任何签名将函数传递给:

fn()
fn(tokens)
fn(locn,tokens)
fn(srctring,locn,tokens)

根据您的需要,您只需要位置和解析的令牌。

添加此解析操作后,您的解析结果现在如下所示:

[(0, '1'), (2, '2'), (4, '34')]

编辑:

自从我对这篇文章的最初回答以来,我已经添加了 pyparsinglocatedExpr助手,它将给出特定表达式的开始和结束位置。现在这可以简单地写成:

NUMBER = locatedExpr(Word(nums))

这是完整的脚本/输出:

>>> from pyparsing import *
... NUMBER = locatedExpr(Word( nums ))
... STATEMENT = ZeroOrMore( NUMBER )
... print( STATEMENT.parseString( "1 2 34" ).dump() )

[[0, '1', 1], [2, '2', 3], [4, '34', 6]]
[0]:
  [0, '1', 1]
  - locn_end: 1
  - locn_start: 0
  - value: '1'
[1]:
  [2, '2', 3]
  - locn_end: 3
  - locn_start: 2
  - value: '2'
[2]:
  [4, '34', 6]
  - locn_end: 6
  - locn_start: 4
  - value: '34'
于 2013-03-08T17:28:32.403 回答