2

我正在尝试解析一个可能包含一些转义字符的字符串,例如 \" \". 例如,

"this is an \"example\" of what I want to parse"

我目前有以下解析规则,但它无法处理转义字符\" QuotedString('"',multiline=True)

escChar 和 escQuote 以及 QuotedString 类有一些选项,但我不确定在那里使用什么。

我想做的一个完整的例子

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', ';']
4

1 回答 1

1

QuotedString 的文档字符串给出:

 |  __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)
 |  

一些交互式解释器实验:

>>> 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'], {})

我不能说所有类的构造函数参数都是 100% 完成的,但可能 > 90%。

于 2012-11-13T20:05:28.190 回答