Terry
Terry

Reputation: 5282

Why does my regex match this and how can I prevent it from doing that

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:

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.

Upvotes: 1

Views: 53

Answers (1)

codaddict
codaddict

Reputation: 455380

The regex metacharacter | has the least precedence. As a result:

^r1|r2$

means match a string that begins with r1 or ends with r2.

Alternatively

^(r1|r2)$

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 200 from you input is being matched. To fix that enclose your regex in a (...) as done in type 2 above.

Upvotes: 2

Related Questions