I have a function which can take a string, interpret it as a calculation and return the result of the calculation, examples for valid calculations are:
3(log(e+pi^(22/3)))
44+3*(pi+2)/root(7)
the function is quite heavy so I would like to only run it if the String actually is a calculation. Before I added functions like log and root, pi and e and implicit multiplycation i used the following regex:
/^((-?([0-9]+?\.)?[0-9]+?)\s?([+\-*\/%^]|(\*\*))\s?)+?(-?([0-9]+?\.)?[0-9]+?)$/
which doesn't work anymore. At this point I am not even sure if a regex would make sense performance wise. I expect about 0.1% of strings to match (being a valid calculation).
Do you have any ideas on how to create a well performing regular expression (the function itself determines weather its a valid calculation itself, but it takes a long time, so no 100% accuracy needed) or a function which validates the calculation?