I’m using a regex to validate formulas (excel-like). Those formulas can contain string (between quotes), cell references (indicated in C[] placeholders –> C[A4]) or integer values. They are concatenated by the “&” char. Here are a few examples of valid formulas:
- =C[A4]+C[B4]
- =”This is text and I can type whatever I want, like this integer value: “&123456789
- =”Start”&(C[A4]+C[A3])/2&123&”End”
This is the regex I’m using:
^-?(\d+)|(C\[[A-Z][0-9]\])([-\+\*\/][\(]*-?((\d+)|(C\[[A-Z][0-9]\]))[\)]*)*$
Untill now, it has done what it was supposed to do. I also have an autocorrect which puts quotes around input that can’t be validated as formula. So when I enter
=200X-8
it should put quotes around the content after the “=”. but this doesn’t happen. My regex.IsMatch seems to be succesfull, allthough I don’t allow X’s in according to the regex. It seems it matches “200” and then nothing more. The weird thing is that I’m indicating I want the full input to be validated (see “$” at the end of the regex).
Am I missing something (I guess the regex is correct), but I don’t want regex.Ismatch to be “succes” when it doesn’t match the full input.
The regex metacharacter
|has the least precedence. As a result:means match a string that begins with r1 or ends with r2.
Alternatively
means match a string which begins and ends with a r1 or r2.
Your existing regex is similar to type 1 above and as a result of that
200from you input is being matched. To fix that enclose your regex in a(...)as done in type 2 above.