Let's suppose to have this sum of products boolean expresion in prefix form:
str_formula = 'Or(A, And(B, C, D), E, F, And(G, H))'
Or
and And
are the only operators permitted and there is no more nesting structure than the one representing above . I would like to rewrite the expression in order to receive the infix form.
My idea, without using regular expressions, was:
import ast
import re
or_op = ' OR '
and_op = ' AND '
str_formula = str_formula.replace('Or(', '[').replace('And(', '[').replace(')', ']')
s_list = re.sub('(\w+)', r"'\1'", str_formula)
list_formula = [x if isinstance(x, list) else [x] for x in ast.literal_eval(s_list)]
infix_form = or_op.join([and_op.join(sublist) for sublist in list_formula])
The infix_form variable is:
'A OR B AND C AND D OR E OR F OR G AND H'
Can you suggest a regex in order to solve in a smarter way this problem?
Consider the following approach using
re.sub()
function with replacement callbackreplaceOperands
:The output: