4

I have a boolean expression string, that I would like to take apart:

condition = "a and (b or (c and d))"

Or let's say:
I want to be able to access the string contents between two parenthesis.
I want following outcome:

"(b or (c and d))"
"(c and d)"

I've tried the following with regular expressions (not really working)

x = re.match(".*(\(.*\))", condition)
print x.group(1)

Question:
What is the nicest way to take a boolean expression string apart?

4

4 回答 4

8

这是你不能用简单的正则表达式做的事情。您需要实际解析文本。pyparsing显然非常适合这样做。

于 2013-04-24T13:15:46.790 回答
2

就像大家说的,你需要一个解析器。

如果不想安装,可以从这个简单的自顶向下解析器开始(这里取最后一个代码示例)

删除与您的需要无关的所有内容(+、-、*、/、is、lambda、if、else、...)。只保留括号, and, or. 您将获得从您的表达式生成的二叉树结构。标记器使用内置tokenize( import tokenize),它是 Python 源代码的词法扫描器,但对于像您这样的简单情况也能正常工作。

于 2013-04-24T13:32:44.350 回答
2

如果您的要求相当简单,那么您实际上并不需要解析器。使用堆栈可以轻松实现匹配括号。

您可以执行以下操作:

condition = "a and (b or (c and d))"

stack = []

for c in condition:
    if c != ')':
        stack.append(c)
    else:
        d = c
        contents = []
        while d != '(':
            contents.insert(0, d)
            d = stack.pop()
        contents.insert(0, d)
        s = ''.join(contents)
        print(s)
        stack.append(s)

产生:

(c and d)
(b or (c and d))
于 2013-04-24T14:14:18.337 回答
0

构建解析器:

 Condition ::= Term Condition'
 Condition' ::= epsilon | OR Term Condition'
 Term  ::=  Factor Term'
 Term' ::= epsilon | AND Factor Term'
 Factor ::= [ NOT ] Primary
 Primary ::= Literal | '(' Condition ')'
 Literal ::= Id
于 2013-04-24T13:15:12.577 回答