Validating a mathematical expression in java
<p>I am trying to validate if the string "expression" as in the code below is a formula.</p>

<pre><code> String expression = request.getParameter(FORMULA);
 if(!Pattern.matches("[a-zA-Z0-9+-*/()]", expression)){return new AjaxMessage(AjaxMessage.ResponseStatusEnum.FAILURE, getJsonString(, "Manager.invalid.formula" , null));
}
</code></pre>

<p>examples of value for <strong>expression</strong> are <strong>{a+b/2, (a+b)*2,(john-Max),etc}</strong> just for the context (the variable names in the formula might vary and the arithmetic expression contains only <strong>[+-/()*]</strong> special characters. As you can see I tried to validate using regex (new to regex), but I think it's not possible as I don't know the length of the variable names.</p>

<p>Is there a way to achieve a validation using regex or any other library in java? </p>

<p>Thanks in advance.</p>