【发布时间】:2015-06-18 14:21:27
【问题描述】:
我有一个字符串,其中包含我试图用 pyparsing 解析的单词列表。
列表始终至少包含三个项目。由此我希望 pyparsing 生成三个组,其中第一个包含直到最后两项的所有单词,最后两组应该是最后两项。例如:
"one two three four"
应该被解析成类似的东西:
["one two"], "three", "four"
我可以用正则表达式做到这一点:
import pyparsing as pp
data = "one two three four"
grammar = pp.Regex(r"(?P<first>(\w+\W?)+)\s(?P<penultimate>\w+) (?P<ultimate>\w+)")
print(grammar.parseString(data).dump())
给出:
['one two three four']
- first: one two
- penultimate: three
- ultimate: four
我的问题是,由于 pyparsing 贪婪的性质,我无法使用非正则表达式 ParserElement 获得相同的结果,例如以下内容:
import pyparsing as pp
data = "one two three four"
word = pp.Word(pp.alphas)
grammar = pp.Group(pp.OneOrMore(word))("first") + word("penultimate") + word("ultimate")
grammar.parseString(data)
回溯失败:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/pyparsing.py", line 1125, in parseString
raise exc
pyparsing.ParseException: Expected W:(abcd...) (at char 18), (line:1, col:19)
因为 OneOrMore 会吞掉列表中的所有单词。到目前为止,我试图通过 FollowedBy 或 NotAny 来防止这种贪婪行为的尝试都失败了 - 关于如何获得所需行为的任何建议?
【问题讨论】:
标签: python pyparsing non-greedy