Next / Previous / Contents / Shipman's homepage

5.37. WordStart: Match only at the start of a word

pp.WordStart(wordChars=pp.printables)

An instance of this class matches only when the current position is at the beginning of a word, and the previous character (if there is one) is not a word character.

The optional wordChars argument specifies which characters are considered word characters; the default value is the set of all printable, non-whitespace characters.

>>> goFour = pp.GoToColumn(5)
>>> letters = pp.Word(pp.alphas)
>>> pat = goFour + pp.WordStart(pp.alphas) + letters
>>> print pat.parseString('1234abcd')
['1234', 'abcd']
>>> print pat.parseString('123zabcd')
pyparsing.ParseException: Not at the start of a word (at char 4),
(line:1, col:5)
>>> firstName = pp.WordStart() + pp.Word(pp.alphas)
>>> print firstName.parseString('Lambert')
['Lambert']
>>> badNews = pp.Word(pp.alphas) + firstName
>>> print badNews.parseString('MrLambert')
pyparsing.ParseException: Not at the start of a word (at char 9),
(line:1, col:10)
>>> print badNews.parseString('Mr Lambert')
['Mr', 'Lambert']