基于此语法:
from pyparsing import *
g = quotedString.setParseAction( removeQuotes )
eg = Suppress('-') + quotedString.setParseAction( removeQuotes )
choice = Or( [ g.setResultsName("out",listAllMatches=True),
eg.setResultsName("in",listAllMatches=True) ] )
grammar = ZeroOrMore( choice ) + Suppress(restOfLine)
a = world.parseString( ' "ali" -"baba" "holy cow" -"smoking beaute" ' )
print a.dump()
我发现满足eg
非终结符的标记总是包含在一个额外的列表中。唯一的区别g
是它有一个领先的`Suppress('-')'。
['ali', 'baba', 'holy cow', 'smoking beaute']
- in: [['baba'], ['smoking beaute']]
- out: ['ali', 'holy cow']
如何使它们的行为相同?我想达到以下结果:
['ali', 'baba', 'holy cow', 'smoking beaute']
- in: ['baba', 'smoking beaute']
- out: ['ali', 'holy cow']