1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
lmriccardo
  • 87
  • 8
  • 1
    Strange that the marked, quick to dismiss duplicate suggests not to use regex for balanced text. The post was 12 years ago back at a time people hated regex. The marked answer warns of not using regex for balanced text as well as 90% of the comments. From 12 years ago ! Not only does that not apply today, but your question is thrown down the toilet. It's not just that you ask for help for balanced text but these questions are always always about the content of the text where each problem is unique. Balanced text is a reality in Python. Unfortunately your question is not allowed to be answered. – sln Jun 16 '23 at 21:01

0 Answers0