Check if it's a mathematical function
Posted: Mon Apr 25, 2011 8:28 am
Hi.
I'm new here.
My knowledge about regex is very limited.
The case:
The user should input a mathematical input like this:
2.3+exp(.4*VAR)-3
(The function is f(VAR).
I have done a very simple regex that checks that there is no mistakes: There are numbers, symbols (+-*/) and the "accepted" functions.
The functions that are allowed are: pow(x,y), exp(3), pi()
My regex is:
/^((exp\(|pow\(|\()?((\d*\.?\d*)|VAR|PI\(\))\}*([,\+\-\*\/]|$))+$/
If the function isn't all right, the eval() function gives me a warning. It works.... but isn't nice).
The mistake cases (the regex says they are fine):
* Different number of '(' than ')'. f=2*(3-4))
* Functions without the right parameters: pow(5)
* Function ends with a symbol: 3+4* or 3+exp(
I hope you can help me.
Thanks.
PS: Sorry for my bad english.
I'm new here.
My knowledge about regex is very limited.
The case:
The user should input a mathematical input like this:
2.3+exp(.4*VAR)-3
(The function is f(VAR).
I have done a very simple regex that checks that there is no mistakes: There are numbers, symbols (+-*/) and the "accepted" functions.
The functions that are allowed are: pow(x,y), exp(3), pi()
My regex is:
/^((exp\(|pow\(|\()?((\d*\.?\d*)|VAR|PI\(\))\}*([,\+\-\*\/]|$))+$/
If the function isn't all right, the eval() function gives me a warning. It works.... but isn't nice).
The mistake cases (the regex says they are fine):
* Different number of '(' than ')'. f=2*(3-4))
* Functions without the right parameters: pow(5)
* Function ends with a symbol: 3+4* or 3+exp(
I hope you can help me.
Thanks.
PS: Sorry for my bad english.