I have the following problem.
I have expressions like this one "or(x) & and() | xor(and() & or())"
.
The idea is to replace each function "or", "and" and "xor" into "func_or", "func_and" and "func_xor". The simplest code that reach the goal in my mind is this one:
expression = "or(x) & and() | xor(and() & or())"
starting_expression = expression
continue = True
while continue:
expression = replace_functions(expression)
continue = not (expression == starting_expression)
starting_expression = expression
At the end obtaining the final expression "func_or(x) & func_and() | func_xor(func_and() & func_or())"
.
The starting point is that I cannot just do expression.replace("or", "func_or")
because I can have in expression like this variables like var_or
. Moreover, after the first iteration all the new functions "func_or"
will become "func_func_or"
and this is not the correct behaviour.
For these reasons, I decided to use RegEx and in particular the method re.sub
because it gives me the possibility to define a function repl
that for each match returns the substitutions to do.
The problems is that I cannot end up with a valid RegEx.
Can someone help me?
Thanks u All.