Next / Previous / Contents / Shipman's homepage

5.36. WordEnd: Match only at the end of a word

pp.WordEnd(wordChars=pp.printables)

An instance of this class matches only when the previous character (if there is one) is a word character and the next character 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.

>>> name4 = pp.Word(pp.alphas, exact=4)
>>> name = pp.Word(pp.alphas)
>>> pat = name4 + pp.WordEnd() + name
>>> print pat.parseString('fire truck')
['fire', 'truck']
>>> print pat.parseString('firetruck')
pyparsing.ParseException: Not at the end of a word (at char 4), (line:1,
col:5)
>>> pat2 = name4 + pp.WordEnd(pp.alphas) + pp.Word(pp.nums)
>>> print pat2.parseString('Doug86')
['Doug', '86']