Use this function to create a string that you can pass to
pp.Word() to create a parser that will match any one of a
specified sets of characters. The argument allows you to
use ranges of character codes so that you don't have to
specify every single character. The syntax of the
argument is similar to the “
[...]” construct of general-purpose regular
ranges argument string
consists of one or more occurrences of:
A backslash followed by a single character, so that
your parser can match characters such as
A character specified by its hexadecimal character code
A character specified by its octal character code as
be one, two or three octal digits.
Two of the above choices separated by
'-', meaning all the characters with codes between those
values, including the endpoints. For example,
pp.srange('[a-z]') will return a parser that
will match any lowercase letter.
Here's an example that demonstrates the use of this function in creating a parser for a Python identifier.
>>> first = pp.Word(pp.srange('[_a-zA-Z]'), exact=1) >>> rest = pp.Optional(pp.Word(pp.srange('[_0-9a-zA-Zz]'))) >>> ident = pp.Combine(first + rest) >>> print ident.parseString('runcorn_Abbey') ['runcorn_Abbey'] >>> print ident.parseString('N23') ['N23'] >>> print ident.parseString('0xy') pyparsing.ParseException: Expected W:(_abc...) (at char 0), (line:1, col:1)