Next / Previous / Contents / Shipman's homepage

6.11. matchPreviousLiteral(): Match the literal text that the preceding expression matched

pp.matchPreviousLiteral(parser)

This function works like the one described in Section 6.10, “matchPreviousExpr(): Match the text that the preceding expression matched”, except that the returned parser matches the exact characters that parser matched, without regard for any following context. Compare the example below with the one in Section 6.10, “matchPreviousExpr(): Match the text that the preceding expression matched”.

>>> name = pp.Word(pp.alphas)
>>> name2 = pp.matchPreviousLiteral(name)
>>> dash2 = pp.Combine(name + pp.Literal('-') + name2)
>>> print dash2.parseString('foo-foofaraw')
['foo-foo']
>>> print dash2.parseString('foo-foofaraw', parseAll=True)
pyparsing.ParseException: Expected end of text (at char 7), (line:1, col:8)