Reputation: 1007
I am trying to parse a string that may consist of some escape characters like \" \"
. For example,
"this is an \"example\" of what I want to parse"
I currently have the following parse rule but it cannot handle the escape characters \"
QuotedString('"',multiline=True)
There are options for escChar and escQuote and such for the QuotedString class but I am not sure what to use there.
A complete example of what I want to do
def test1():
str_ = QuotedString('"',escChar='\\',multiline=True)
decl = (Keyword("FIELD1") + str_ + ';') | \
(Keyword("FIELD2") + str_ + ';')
G = OneOrMore(decl)
s = """
FIELD1 "hello world";
FIELD2 "an example of \"what\" I want to parse";
"""
print G.parseString(s)
# Only print ['FIELD1', 'hello \nworld', ';']
Upvotes: 2
Views: 1318
Reputation: 63747
The docstring for QuotedString gives:
| __init__(self, quoteChar, escChar=None, escQuote=None, multiline=False, unquoteResults=True, endQuoteChar=None)
| Defined with the following parameters:
| - quoteChar - string of one or more characters defining the quote delimiting string
| - escChar - character to escape quotes, typically backslash (default=None)
| - escQuote - special quote sequence to escape an embedded quote string (such as SQL's "" to escape an embedded ") (default=None)
| - multiline - boolean indicating whether quotes can span multiple lines (default=False)
| - unquoteResults - boolean indicating whether the matched text should be unquoted (default=True)
| - endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=None => same as quoteChar)
|
Some interactive interpreter experiments:
>>> import pyparsing
>>> s = r'''"this is an \"example\" of what I want to parse" '''
>>> pyparsing.QuotedString('"').parseString(s)
(['this is an \\'], {})
>>> pyparsing.QuotedString('"', escChar='\\').parseString(s)
(['this is an "example" of what I want to parse'], {})
I can't say that the constructor args for all the classes are 100% complete, but probably >90%.
Upvotes: 1