Next / Previous / Contents / Shipman's homepage

7.13. lineStart: An instance of LineStart

The variable pp.lineStart is an instance of Section 5.16, “LineStart: Match start of line”.

In this example, pattern initialWord matches a string of letters at the beginning of a line. The sample text matches because the beginning of the string is considered the beginning of a line.

>>> initialWord = pp.lineStart + pp.Word(pp.alphas)
>>> print initialWord.parseString('Silly party\n')
['Silly']

In the next example, pattern pat1 matches a word, followed by the end of a line, followed by pattern initialWord. The first sample text matches because the position just after a '\n' is considered the start of a line.

>>> pat1 = pp.Word(pp.alphas) + pp.lineEnd + initialWord
>>> print pat1.parseString('Very\nSilly Party')
['Very', '\n', 'Silly']

This example fails to match. The first part of pat2 matches the word 'Harpenden'; automatic blank skipping moves to the beginning of the word 'southeast'; and then the pp.lineStart parser fails because the space before 'southeast' is not a newline or the beginning of the string.

>>> pat2 = pp.Word(pp.alphas) + initialWord
>>> print pat2.parseString('Harpenden southeast')
pyparsing.ParseException: Expected lineStart (at char 9), (line:1, col:10)