2011-04-24 54 views

回答

3

你能用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'], {}) 
+0

'Combine'不會爲'from:mary'拋出'ParseException'。我想要拋出錯誤。 – 2011-04-24 05:21:48

+0

它的工作原理。我正在陷入異常。 – 2011-04-24 13:31:43

+2

此外,請閱讀'leaveWhitespace'方法以禁止特定表達式的空白跳過。在任何必須匹配而不跳過空格的表達式上調用這個方法,在你的情況下是':'和包含名字的單詞。 – PaulMcG 2011-04-24 14:44:05

相關問題