I have a regular expression in c# that should return IsMatch = true only when the input has the desired pattern but actually is returning true if some of the characters matches...How would be the correct regular expression?
Regex reg = new Regex(@"[0-9 \-+]"); //accept only numbers, spaces, minus character and plus character
string formularight="1123 - 4432+32124";//its correct
bool validformat=reg.IsMatch(formularight)) //returns true, ok
string formulawrong="1123a - 4432+32124"; //it has one letter ismatch should be false...
validformat=reg.IsMatch(formulawrong)) //returns true, not ok
I check later if each number is followed by a minus or plus sign before the next number but if it can be included in the regex validation...
I checked other regex questions and before someone suggest that i use a datatable to compute() the expresion or use some calculator logic please know that in this case the numbers are used like fields names that i will use to get some values from the database not numerical values per se. So i only need the regex validation before parsing the formula. Thanks
Valid examples for regex:
11123
112 - 1121
112-1121
1221111+554-111135678
44332-54-114
Invalid examples (letters present, not a + or - between numbers,...):
112 -
6543e
112 1121
6543e + 4432
-7632