How to prevent spaces between literals in pyparsing?

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

Grammar should reject from : maryand accept only from:mary, i.e. without any intermittent spaces. How can I apply this in pyparsing? Thanks

+5
source share
1 answer

Can you use Combine?

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

So then:

EDIT in response to your comment.

Really?

>>> 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'], {})
+3
source

All Articles