I'm looping through thousands of strings with various regexes to check for simple errors. I would like to add a regex to check for the correct use of commas.
If a comma exists in one of my strings, then it MUST be followed by either whitespace or exactly three digits:
- valid: ,\s
- valid: ,\d\d\d
But if a comma is followed by any other pattern, then it is an error:
- invalid: ,\D
- invalid: ,\d
- invalid: ,\d\d
- invalid: ,\d\d\d\d
The best regex I've come up with thus far is:
Regex CommaError = new Regex(@",(^(\d\d\d)|\S)"); // fails case #2
To test, I am using:
if (CommaError.IsMatch(", ")) // should NOT match
Console.WriteLine("failed case #1");
if (CommaError.IsMatch(",234")) // should NOT match
Console.WriteLine("failed case #2");
if (!CommaError.IsMatch("0,a")) // should match
Console.WriteLine("failed case #3");
if (!CommaError.IsMatch("0,0")) // should match
Console.WriteLine("failed case #4");
if (!CommaError.IsMatch("0,0a1")) // should match
Console.WriteLine("failed case #5");
But the regex I gave above fails case #2 (it matches when it should not).
I've invested several hours investigating this, and searched the Web for similar regexes, but have hit a brick wall. What's wrong with my regex?
Update: Peter posted a comment with a regex that works the way I want:
Regex CommaError = new Regex(@",(?!\d\d\d|\s)");
Edit: Well, almost. It fails in this case:
if (!CommaError.IsMatch("1,2345")) // should match
Console.WriteLine("failed case #6");