Next / Previous / Contents / Shipman's homepage

6.14. srange(): Specify ranges of characters


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 expressions.

The ranges argument string consists of one or more occurrences of:

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')
>>> print ident.parseString('N23')
>>> print ident.parseString('0xy')
pyparsing.ParseException: Expected W:(_abc...) (at char 0), (line:1,