Next / Previous / Contents / Shipman's homepage

5.27. QuotedString: Match a delimited string

pp.QuotedString(quoteChar, escChar=None, multiline=False,
    unquoteResults=True, endQuoteChar=None)

An instance of this class matches a string literal that is delimited by some quote character or characters.

quoteChar

This string argument defines the opening delimiter and, unless you pass an endQuoteChar argument, also the closing delimiter. The value may have multiple characters.

escChar

Strings may not normally include the closing quote character inside the string. To allow closing quote characters inside the string, pass an argument escChar=c, where c is an escape character that signifies that the following character is to be treated as text and not as a delimiter.

multiline

By default, a string may not include newline characters. If you want to allow the parser to match quoted strings that extend over multiple lines, pass an argument “multiline=True”.

>>> qs = pp.QuotedString('"')
>>> print qs.parseString('"semprini"')
['semprini']
>>> cc = pp.QuotedString('/*', endQuoteChar='*/')
>>> print cc.parseString("/* Attila the Bun */")
[' Attila the Bun ']
>>> pat = pp.QuotedString('"', escChar='\\')
>>> print pat.parseString(r'"abc\"def"')
['abc"def']
>>> text = """'Ken
... Obvious'"""
>>> print text
'Ken
Obvious'
>>> pat = pp.QuotedString("'")
>>> print pat.parseString(text)
pyparsing.ParseException: Expected quoted string, starting with ' ending
with ' (at char 0), (line:1, col:1)
>>> pat = pp.QuotedString("'", multiline=True)
>>> print pat.parseString(text)
['Ken\nObvious']
>>> pat = pp.QuotedString('|')
>>> print pat.parseString('|clever sheep|')
['clever sheep']
>>> pat = pp.QuotedString('|', unquoteResults=False)
>>> print pat.parseString('|clever sheep|')
['|clever sheep|']