8
grammar = Literal("from") + Literal(":") + Word(alphas)

语法需要拒绝from : mary并且只接受from:mary即没有任何交错空格。如何在 pyparsing 中强制执行此操作?谢谢

4

1 回答 1

7

你能用Combine吗?

grammar = Combine(Literal("from") + Literal(":") + Word(alphas))

那么:

编辑以回应您的评论。

真的吗?

>>> grammar = pyparsing.Combine(Literal("from") + Literal(":") + Word(pyparsing.alphas))
>>> grammar.parseString('from : mary')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/pyparsing.py", line 1076, in parseString
    raise exc
pyparsing.ParseException: Expected ":" (at char 4), (line:1, col:5)
>>> grammar.parseString('from:mary')
(['from:mary'], {})
于 2011-04-24T04:21:55.860 回答