.net - How to Validate mathematical formula expressions using regex -
i looking mathematical formula validation using regex. here illustration code using:-
string equation = "(a+b)*10"; bool b = false; regex rx = new regex(@"^((?<o1>[^-+*/()]+|\([^-+*/()]+\)|(?<p>\()+[^-+*/()]+|[^-+*/()]+(?<-p>\)))[-+*/])+(?<o2>[^-+*/()]+|\([^-+*/()]+\)|(?<p>\()+[^-+*/()]+|[^-+*/()]+(?<-p>\)))(?(p)(?!))$"); if (rx.ismatch(equation)) { b = true; }
it working fine look used "(a+b)*100". mean if go wrong placement of braces "(a+b))*100" or "(a+b*100" or "(a+b)*100)" doing perfect validation. if go introduction of user defined functions "sum(1,2)" or sqrt(8) failing validate.
i want validate functions also. "sum(1,2)","sum(1,2" ,"sum1,2)", "sum(1,2))". mean should take care of proper usage of braces.
note:- using antlr create these funations.
thanks
a generic mathematical formula complex parse using regular expression. utilize antlr write grammar can parse out individual elements instead.
.net regex
No comments:
Post a Comment