Regex in boolean expression

493 views Asked by At

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?

1

There are 1 answers

1
RomanPerekhrest On

Consider the following approach using re.sub() function with replacement callback replaceOperands:

str_formula = 'Or(A, And(B, C, D), E, F, And(G, H))'

def replaceOperands(m):
    s = re.sub(r'\(|\)', '', m.group(2).replace(',', ' OR')) if m.group(1) == 'Or' else '('+m.group(2).replace(',', ' AND')+')'
    return s

str_formula = re.sub(r'\b(Or)\(([A-Z], (?:\(.*\))[A-Z]?)', 
                     replaceOperands, 
                     re.sub(r'\b(And)\(([^)]+)\)', replaceOperands, str_formula))
print(str_formula)

The output:

A OR B AND C AND D OR E OR F OR G AND H