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
, 07
and AA
to end right there ($
matches the end of input). Remove the $
from these sub-expressions, and move it to the end of the expression.
"^[0-9](03|07|AA|[A-Z])[A-Z]{2}$"
I believe this is because you are using the "$" in the regex, which means in this case to assert position at the end of a line (at the end of the string or before a line break character). Remove it and it should work. From Regex Buddy, here is what you were doing:
([0-9]{1})((03$)|(07$)|(AA$)|[A-Z]{1})([A-Z]{2})
Options: ^ and $ match at line breaks Match the regular expression below and capture its match into backreference number 1 «([0-9]{1})» Match a single character in the range between “0” and “9” «[0-9]{1}» Exactly 1 times «{1}» Match the regular expression below and capture its match into backreference number 2 «((03$)|(07$)|(AA$)|[A-Z]{1})» Match either the regular expression below (attempting the next alternative only if this one fails) «(03$)» Match the regular expression below and capture its match into backreference number 3 «(03$)» Match the characters “03” literally «03» Assert position at the end of a line (at the end of the string or before a line break character) «$» Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(07$)» Match the regular expression below and capture its match into backreference number 4 «(07$)» Match the characters “07” literally «07» Assert position at the end of a line (at the end of the string or before a line break character) «$» Or match regular expression number 3 below (attempting the next alternative only if this one fails) «(AA$)» Match the regular expression below and capture its match into backreference number 5 «(AA$)» Match the characters “AA” literally «AA» Assert position at the end of a line (at the end of the string or before a line break character) «$» Or match regular expression number 4 below (the entire group fails if this one fails to match) «[A-Z]{1}» Match a single character in the range between “A” and “Z” «[A-Z]{1}» Exactly 1 times «{1}» Match the regular expression below and capture its match into backreference number 6 «([A-Z]{2})» Match a single character in the range between “A” and “Z” «[A-Z]{2}» Exactly 2 times «{2}»
Followed by the revised version:
([0-9]{1})((03)|(07)|(AA)|[A-Z]{1})([A-Z]{2})
Options: ^ and $ match at line breaks Match the regular expression below and capture its match into backreference number 1 «([0-9]{1})» Match a single character in the range between “0” and “9” «[0-9]{1}» Exactly 1 times «{1}» Match the regular expression below and capture its match into backreference number 2 «((03)|(07)|(AA)|[A-Z]{1})» Match either the regular expression below (attempting the next alternative only if this one fails) «(03)» Match the regular expression below and capture its match into backreference number 3 «(03)» Match the characters “03” literally «03» Or match regular expression number 2 below (attempting the next alternative only if this one fails) «(07)» Match the regular expression below and capture its match into backreference number 4 «(07)» Match the characters “07” literally «07» Or match regular expression number 3 below (attempting the next alternative only if this one fails) «(AA)» Match the regular expression below and capture its match into backreference number 5 «(AA)» Match the characters “AA” literally «AA» Or match regular expression number 4 below (the entire group fails if this one fails to match) «[A-Z]{1}» Match a single character in the range between “A” and “Z” «[A-Z]{1}» Exactly 1 times «{1}» Match the regular expression below and capture its match into backreference number 6 «([A-Z]{2})» Match a single character in the range between “A” and “Z” «[A-Z]{2}» Exactly 2 times «{2}»
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With