我正在尝试解析可能包含一些转义字符的字符串,例如 \"\"。例如,
“这是我要解析的‘示例’”
我目前有以下解析规则,但它无法处理转义字符 \" 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', ';']
请您参考如下方法:
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%。
