Next / Previous / Contents / Shipman's homepage

7.17. pythonStyleComment: Comments in the style of the Python language

The variable pp.pythonStyleComments is a parser that matches comments in the style of the Python programming language: a comment begins with “#” and ends at the end of the line (or the end of the entire text string, whichever comes first).

>>> print pp.pythonStyleComment.parseString('# The Silly Party take Luton.')
['# The Silly Party take Luton.']
>>> codePart = pp.ZeroOrMore(pp.CharsNotIn('#\n'))
>>> commentPart = pp.pythonStyleComment
>>> line = codePart + pp.Optional(commentPart)
>>> print line.parseString('    nVikings = 0')
['    nVikings = 0']
>>> print line.parseString('    nVikings = 0   # Reset Viking count')
['    nVikings = 0   ', '# Reset Viking count']