regex - Validating a mathematical expression in java -


i trying validate if string "expression" in code below formula.

 string expression = request.getparameter(formula);  if(!pattern.matches("[a-za-z0-9+-*/()]", expression)){return new ajaxmessage(ajaxmessage.responsestatusenum.failure, getjsonstring(, "manager.invalid.formula" , null)); } 

examples of value expression {a+b/2, (a+b)*2,(john-max),etc} context (the variable names in formula might vary , arithmetic expression contains [+-/()*] special characters. can see tried validate using regex (new regex), think it's not possible don't know length of variable names.

is there way achieve validation using regex or other library in java?

thanks in advance.

the reason using characters special meaning in regex. need escape characters. have modified yor regex make work.

code:

list<string> expressions = new arraylist<string>();     expressions.add("a+b/2");     expressions.add("(a+b)*2");     expressions.add("john-max");     expressions.add("etc[");     (string expression : expressions) {         if (!pattern.matches("[a-za-z0-9\\+\\-\\*/\\(\\)]*", expression)) {             system.out.println("not match");         } else {             system.out.println("match");         }     }  } 

output:

match match match not match 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -