在Pyparsing中,MatchFirst、Or和oneOf有什么不同
当字符串中有共享字符时,如
单词,措辞,单词
Or('word','wording','words')
MatchFirst('word','wording','words')
oneOf('word','wording','words')
发布于 2017-03-06 21:31:22
oneOf对被理解为空格分隔字符串的str进行操作,可以简单地定义为
oneOf = lambda xs: Or(Literal(x) for x in xs.split(" "))而Or在expressions ParseElement实例上运行。因此,您可以将oneOf看作是Or的特化,或者将Or看作是oneOf的泛化。
您可以将oneOf('foo bar')编写为Literal('foo') ^ Literal('bar'),但不能使用oneOf编写每个Or表达式。
除了冲突解决方法之外,MatchFirst与Or相同- Or产生最长的匹配,而MatchFirst按定义顺序返回第一个匹配。
所以
expr = Literal('bar') ^ Words(alphanums)
expr.parseString("barstool").asList() == ["barstool"] 但
expr = Literal('bar') | Words(alphanums)
expr.parseString("barstool").asList() == ["bar"]https://stackoverflow.com/questions/24358037
复制相似问题