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.

3
  • 1
    All you want to do is add quotes inside the parentheses sometimes? Commented May 20, 2013 at 0:53
  • 1
    It is not clear what you are asking. How is decided when the quotes are added? Commented May 20, 2013 at 0:53
  • 5
    I would suggest accepting some answers 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. Commented May 20, 2013 at 0:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.