A simple regexp in python
Question: My program is a simple calculator, so I need to parse te expression which the
user types, to get the input more user-friendly. I know I can do it with
regular expressions, but I'm not familar enough about this.

So I need transform a input like this:

    
    
    import re
    input_user = "23.40*1200*(12.00-0.01)*MM(H2O)/(8.314 *func(2*x+273.15,x))"
    re.some_stuff( ,input_user) # ????
    

in this:

    
    
    "23.40*1200*(12.00-0.01)*MM('H2O')/(8.314 *func('2*x+273.15',x))"
    

just adding these simple quotes inside the parentheses. How can I do that?

UPDATE:

To be more clear, I want add simple quotes after every sequence of characters
"MM(" and before the ")" which comes after it, and after every sequence of
characters "func(" and before the "," which comes after it.


Answer: This is the sort of thing where regexes can work, but they can potentially
result in major problems unless you consider exactly what your input will be
like. For example, can whatever is inside MM(...) contain parentheses of its
own? Can the first expression in func( contain a comma? If the answers to both
questions is no, then the following could work:

    
    
     input_user2 = re.sub(r'MM\(([^\)]*)\)', r"MM('\1')", input_user)
     output = re.sub(r'func\(([^,]*),', r"func('\1',", input_user)
    

However, this will _not_ work if the answer to either question is yes, and
even without that could cause problems depending upon what sort of inputs you
expect to receive. Essentially, the first re.sub here looks for MM( ('MM('),
followed by any number (including 0) of characters that aren't a close-
parenthesis ('([^)]*)') that are then stored as a group (caused by the extra
parentheses), and then a close-parenthesis. It replaces that section with the
string in the second argument, where \1 is replaced by the first and only
group from the pattern. The second re.sub works similarly, looking for any
number of characters that aren't a comma.

If the answer to either question is yes, then regexps aren't appropriate for
the parsing, as your language would not be regular. [The answer to this
question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-
except-xhtml-self-contained-tags/1732454#1732454), while discussing a
different application, may give more insight into that matter.

