Next / Previous / Contents / Shipman's homepage

6.9. matchOnlyAtCol(): Parse action to limit matches to a specific column

pp.matchOnlyAtCol(col)

Use this function as a parse action to force a parser to match only at a specific column number within the line, counting from 1.

>>> pound2 = pp.Literal('#').setParseAction(pp.matchOnlyAtCol(1))
>>> colorName = pp.Combine(pound2 + pp.Word(pp.hexnums, exact=6))
>>> print colorName.parseString('#00ff88')
['#00ff88']
>>> offColor = pp.Optional(pp.Literal('-')) + colorName
>>> print offColor.parseString('#ff0044')
['#ff0044']
>>> print offColor.parseString('-#ff0044')
pyparsing.ParseException: matched token not at column 1 (at char 1),
(line:1, col:2)