If you want a negative or positive expression, you can write it like this> ^\-?[0-9](([-+/*][0-9]+)?([.,][0-9]+)?)*?$
And the second - ^[(]?[-]?([0-9]+)[)]??([(]?([-+/*]([0-9]))?([.,][0-9]+)?[)]?)*$
With a parenthesis in the expression, but not counting the number, you will need a method that checks it or the regular expression. // method
public static bool IsPairParenthesis(string matrixExpression) { int numberOfParenthesis = 0; foreach (char character in matrixExpression) { if (character == '(') { numberOfParenthesis++; } if (character == ')') { numberOfParenthesis--; } } if (numberOfParenthesis == 0) { return true; } return false; }
source share