title: Put parenthesis in to string python
body: <p>I have an input string like this :</p>
  
  <pre><code>f(x, 3*y) * 54 = 64 / (7 * x) + f(2*x, y-6)
  </code></pre>
  
  <p>I want to get this output:</p>
  
  <pre><code>(x / (3*y)) * 54 = 64 / (7 * x) + ((2*x) / (y-6))
  </code></pre>
  
  <p>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))</p>
  
  <p>I have code that works for most test input but, in some test cases, it generates the wrong output:</p>
  
  <pre><code>line = sub(r"f\((.+?) *, *(.+?)\)", r"(\1 / \2)", (sub(r"f\((.+?[\+-/\*]+.+?) *, *(.+?)\)", r"f((\1),\2)", (sub(r"f\((.+?) *, *(.+?[\+-/\*]+.+?)\)", r"f(\1,(\2))", line)))))
  </code></pre>
  
  <p>This is the code I have written. As I mentioned, it works well, but for this line:</p>
  
  <pre><code>f(x, 3*y) * 54 = 64 / (7 * x) + f(2*x, y-6)
  </code></pre>
  
  <p>I get this result:</p>
  
  <pre><code>((x / (3*y)) * 54 = 64 / (7 * x) + (2*x) / (y-6))
  </code></pre>
  
  <p>One of the parentheses is in the wrong place. I have no idea what the problem is. </p>