我在尝试调试 pyparsing 代码时遇到了这种意外行为:
string1 = "this is a test string : that behaves as I expect\n"
string2 = "this string does not behave as I expect\n"
field = CharsNotIn(":\n")
line = field + ZeroOrMore(Literal(":") + field) + LineEnd()
print line.parseString(string1)
print line.parseString(string2)
这会产生以下输出:
['this is a test string ', ':', ' that behaves as I expect', '\n']
['this string does not behave as I expect']
由于某种原因,解析器能够在 中提取行尾字符string1
,但无法在 中提取它string2
。string2
我什至无法理解如果它没有拿起行尾,它是如何产生匹配的。
这种行为似乎特别适用于行尾字符,因为使用行尾以外的字符似乎可以正常工作:
string1 = "this is a test string : that behaves as I expect*"
string2 = "this string also behaves as I expect*"
field = CharsNotIn(":*")
line = field + ZeroOrMore(Literal(":") + field) + Literal("*")
print line.parseString(string1)
print line.parseString(string2)
这会产生:
['this is a test string ', ':', ' that behaves as I expect', '*']
['this string also behaves as I expect', '*']