0

I would like to remove all non-numeric characters from a string, except operators such as +,-,*,/, and then later evaluate it. For example, suppose the input is 'What is 2+2?' The result should be '2+2' - keeping only the operator symbols and numeric digits.

How can I do this in Python? I tried this so far, but can it be improved?

def evaluate(splitted_cm):
    try:
        splitted_cm = splitted_cm.replace('x', '*').replace('?', '')
        digs = [x.isdigit() for x in splitted_cm]
        t = [i for i, x in enumerate(digs) if x]
        answer = eval(splitted_cm[t[0]:t[-1] + 1])
        return str(answer)

    except Exception as err:
        print(err)
7
  • 1
    expression = ''.join([x for x in splitted_cm if x.isdigit() or x in '+=/*']) Commented Jul 22, 2022 at 16:48
  • 4
    Do not use eval() for anything that could possibly receive input from outside the program in any form. It is a critical security risk that allows the creator of that input to execute arbitrary code on your computer. Commented Jul 22, 2022 at 16:49
  • Please note that this is not a discussion forum. I edited the question to remove noise and ask the question clearly. That said: I'm a bit confused here. It seems as though the code doesn't correspond to the problem requirement at all. The actual evaluation of the expression is a separate process which you don't seem to be asking about, but which is embedded into the code attempt. Commented Jul 22, 2022 at 16:56
  • Also: what should happen for an input like 4 cats + 3 dogs? What does happen? Commented Jul 22, 2022 at 16:57
  • @KarlKnechtel I actually would like to get the output after evaluating that 2+2. Your edit makes it people think, I want the output just like after removing all letters and other chars except numbers and operators. - Also: what should happen for an input like 4 cats + 3 dogs? My current environment does not need such an expression. No args given will be like 4 cats + 3 dogs. The thing I'm working on will only give strings such as "what is 2+2?", "what is 3+3". Just like those. Commented Jul 22, 2022 at 17:15

1 Answer 1

3

You can use regex and re.sub() to make substitutions.

For example:

expression = re.sub("[^\d+-/÷%\*]*", "", text)

will eliminate everything that is not a number or any of +-/÷%*. Obviously, is up to you to make a comprehensive list of the operators you want to keep.

That said, I'm going to paste here @KarlKnechtel's comment, literally:

Do not use eval() for anything that could possibly receive input from outside the program in any form. It is a critical security risk that allows the creator of that input to execute arbitrary code on your computer.

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

1 Comment

Thanks for the advice. I'll make sure to not use eval in codes which I intent to recieve input from strangers.

Your Answer

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