0

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.

David Sousa
  • 383
  • 2
  • 13
  • 1
    All you want to do is add quotes inside the parentheses sometimes? – Explosion Pills May 20 '13 at 00:53
  • 1
    It is not clear what you are asking. How is decided when the quotes are added? – dansalmo May 20 '13 at 00:53
  • 5
    I would suggest [accepting some answers](http://meta.stackexchange.com/a/5235/184971) for your previous questions. It shows appreciation to the many talented developers who have spent their time trying to help you and indicates to users which answer is worth trying out. –  May 20 '13 at 00:54

1 Answers1

2

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, while discussing a different application, may give more insight into that matter.

Community
  • 1
  • 1
cge
  • 9,552
  • 3
  • 32
  • 51
  • Thank you. In the case of "func(", the expression inside don't use extra commas. However in the case of "MM(" it's possible that the expression contains other parentheses. I didn't think about that. Maybe I need modify a bit more this code... – David Sousa May 20 '13 at 02:12