Next / Previous / Contents / Shipman's homepage

5.16. LineStart: Match start of line

pp.LineStart()

An instance of this class matches if the current position is at the beginning of a line; that is, if it is either the beginning of the text or preceded by a newline. It does not advance the current position or contribute any content to the result.

Here are some examples. The first pattern matches a name at the beginning of a line.

>>> name = pp.Word(pp.alphas)
>>> sw = pp.LineStart() + name
>>> print sw.parseString('Dinsdale')
['Dinsdale']

The ansb pattern here matches a name followed by a newline followed by another name. Note that although there are four components, there are only three strings in the result; the pp.LineStart() does not contribute a result string.

>>> ansb = name + pp.LineEnd() + pp.LineStart() + name
>>> print ansb.parseString('Spiny\nNorman')
['Spiny', '\n', 'Norman']

Here is an example of pp.LineStart() failing to match.

>>> asb = name + pp.LineStart() + name
>>> asb.parseString('Bath Wells')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/pyparsing.py", line 1032, in
parseString
    raise exc
pyparsing.ParseException: Expected start of line (at char 4), (line:1,
col:5)

For more examples, see Section 7.13, “lineStart: An instance of LineStart.