Next / Previous / Contents / Shipman's homepage

5.29. SkipTo: Search ahead for a pattern

pp.SkipTo(target, include=False, ignore=None, failOn=None)

An instance of this class will search forward in the input until it finds text that matches a parser target.


By default, when text matching the target pattern is found, the position is left at the beginning of that text. If you specify include=True, the position will be left at the end of the matched text, and the ParseResult will include a two-element list whose first element is the text that was skipped and the second element is the text that matched the target parser.


You can specify a pattern to be ignored while searching for the target by specifying an argument ignore=p, where p is a parser that matches the pattern to be ignored.


You can specify a pattern that must not be skipped over by passing an argument failOn=p, where p is a parser that matches that pattern. If you do this, the SkipTo parser will fail if it ever recognizes input that matches p.

>>> digits = pp.Word(pp.nums)
>>> name = pp.Word(pp.alphas)
>>> ndn = name + pp.SkipTo(digits) + digits + name
>>> print ndn.parseString('Basil%@@^(@^*(83Fawlty')
['Basil', '%@@^(@^*(', '83', 'Fawlty']
>>> nn = name + pp.SkipTo(digits, include=True) + name
>>> print nn.parseString('Basil%@@^(@^*(83Fawlty')
['Basil', ['%@@^(@^*(', '83'], 'Fawlty']