在pyparsing中使用escChar和escQuote

时间:2012-11-13 15:59:39

标签: python pyparsing

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

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

1 个答案:

答案 0 :(得分:1)

QuotedString的docstring给出:

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

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