Find answer to string equation without using eval()
I need a way of taking an equation given as a string and finding it's mathematical answer, the big caveat is that I can't use eval(). I know the equation will only ever contain numbers, the four mathematical operators (i.e. * / + -) and parentheses, it may or may not have spaces in the string. Here's a couple of examples. 4 * 4 4+6/3 (3 / 2)*(4+8) (4+8) * 2 I'm guessing that it's going to have to be done with some kind of regex? Math expressions aren't regular. They're context-free . Your best bet is to parse them using well-known math parsing algorithms like the shunting yard algorithm . All