I have an input string like this :
f(x, 3*y) * 54 = 64 / (7 * x) + f(2*x, y-6)
I want to get this output:
(x / (3*y)) * 54 = 64 / (7 * x) + ((2*x) / (y-6))
The rule: the parenthesis marked with 'f', remove the 'f' and the ',' mark have to change to /. If one of the sides contain an expresion it have to be put in to parenthesis f(2 , 2 + x) = (2 / (2 + x))
I have code that works for most test input but, in some test cases, it generates the wrong output:
line = sub(r"f\((.+?) *, *(.+?)\)", r"(\1 / \2)", (sub(r"f\((.+?[\+-/\*]+.+?) *, *(.+?)\)", r"f((\1),\2)", (sub(r"f\((.+?) *, *(.+?[\+-/\*]+.+?)\)", r"f(\1,(\2))", line)))))
This is the code I have written. As I mentioned, it works well, but for this line:
f(x, 3*y) * 54 = 64 / (7 * x) + f(2*x, y-6)
I get this result:
((x / (3*y)) * 54 = 64 / (7 * x) + (2*x) / (y-6))
One of the parentheses is in the wrong place. I have no idea what the problem is.
.+?matches parens when it shouldn't, and you see that clearly with this compared to this.