Next / Previous / Contents / Shipman's homepage

5.12. GoToColumn: Advance to a specified position in the line

pp.GoToColumn(colNo)

This class returns a parser that causes the input position to advance to column number colNo, where column numbers are counted starting from 1. The value matched by this parser is the string of characters between the current position and position colNo. It is an error if the current position is past column colNo.

>>> pat = pp.Word(pp.alphas, max=4)+pp.GoToColumn(5)+pp.Word(pp.nums)
>>> print pat.parseString('ab@@123')
['ab', '@@', '123']
>>> print pat.parseString('wxyz987')
['wxyz', '', '987']
>>> print pat.parseString('ab  123')
['ab', '', '123']

In this example, pat is a parser with three parts. The first part matches one to four letters. The second part skips to column 5. The third part matches one or more digits.

In the first test, the GoToColumn parser returns '@@' because that was the text between the letters and column 5. In the second test, that parser returns the empty string because there are no characters between 'wxyz' and '987'. In the third example, the part matched by the GoToColumn is empty because white space is ignored between tokens.