I’m trying to use a .NET Regex to validate the input format of a string. The string can be of the format
single digit 0-9 followed by
single letter A-Z OR 07 OR 03 or AA followed by
two letters A-Z
So 0AAA, 107ZF, 503GH, 0AAAA are all valid. The string with which I construct my Regex is as follows:
"([0-9]{1})" +
"((03$)|(07$)|(AA$)|[A-Z]{1})" +
"([A-Z]{2})"
Yet this does not validate strings in which the second term is one of 03, 07 or AA. Whilst debugging, I removed the third term from the string used to construct the regex, and found that input strings of the form 103, 507, 6AA WOULD validate…….
Any ideas why, when I then put the third term back into the Regex, the input strings such as 1AAGM do not match?
Thanks
Tom
This is because your expression requires the strings with
03,07andAAto end right there ($matches the end of input). Remove the$from these sub-expressions, and move it to the end of the expression.