Next / Previous / Contents / Shipman's homepage

5.35. Word: Match characters from a specified set

pp.Word(initChars, bodyChars=None, min=1, max=0,
        exact=0, asKeyword=False, excludeChars=None)

An instance of this class will match multiple characters from a set of characters specified by the arguments.


If no bodyChars argument is given, this argument specifies all the characters that will be matched. If a bodyChars string is supplied, initChars specifies valid initial characters, and characters after the first that are in the bodyChars string will also match.


See initChars.


The minimum length to be matched.


The maximum length to be matched.


If you supply this argument with a value of some number n, this parser will match exactly n characters.


By default, this parser will disregard the text following the matched part. If you specify asKeyword=True, the match will fail if the next character after the matched part is one of the matching characters (a character in initChars if there is no bodyChars argument, or a character in bodyChars if that keyword argument is present).


If supplied, this argument specifies characters not to be considered to match, even if those characters are otherwise considered to match.

>>> name = pp.Word('abcdef')
>>> print name.parseString('fadedglory')
>>> pyName = pp.Word(pp.alphas+'_', bodyChars=pp.alphanums+'_')
>>> print pyName.parseString('_crunchyFrog13')
>>> name4 = pp.Word(pp.alphas, exact=4)
>>> print name4.parseString('Whizzo')
>>> noXY = pp.Word(pp.alphas, excludeChars='xy')
>>> print noXY.parseString('Sussex')