我正在尝试开发一种可以解析以括号开头和结尾括号的表达式的语法。括号内可以有任何字符组合。我已经按照 pyparsing 的 Hello World 程序编写了以下代码。
from pyparsing import *
select = Literal("select")
predicate = "(" + Word(printables) + ")"
selection = select + predicate
print (selection.parseString("select (a)"))
但这会引发错误。我认为这可能是因为 printables 也由 ( 和 ) 组成,并且它与指定的 ( 和)。
正确的做法是什么?
请您参考如下方法:
您可以使用 alpha 代替打印品。
from pyparsing import *
select = Literal("select")
predicate = "(" + Word(alphas) + ")"
selection = select + predicate
print (selection.parseString("select (a)"))
如果使用 { } 作为嵌套字符
from pyparsing import *
expr = Combine(Suppress('select ') + nestedExpr('{', '}'))
value = "select {a(b(c\somethinsdfsdf@#!@$@#@$@#))}"
print( expr.parseString( value ) )
output: [['a(b(c\\somethinsdfsdf@#!@$@#@$@#))']]
( ) 的问题是它们被用作默认的引号字符。
