Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python nicest way to take boolean expression string apart

Tags:

python

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?

like image 892
rebel Avatar asked Dec 15 '22 11:12

rebel


2 Answers

This is the sort of thing you can't do with a simple regex. You need to actually parse the text. pyparsing is apparently excellent for doing that.

like image 97
Daniel Roseman Avatar answered Dec 18 '22 11:12

Daniel Roseman


Like everyone said, you need a parser.

If you don't want to install one, you can start from this simple top-down parser (take the last code sample here)

Remove everything not related to your need (+, -, *, /, is, lambda, if, else, ...). Just keep parenthesis, and, or. You will get a binary tree structure generated from your expression. The tokenizer use the build-in tokenize (import tokenize), which is a lexical scanner for Python source code but works just fine for simple cases like yours.

like image 37
Cyrille Avatar answered Dec 18 '22 11:12

Cyrille