Simple Regular Expression (Regex) (.net)

I am trying to use .NET Regex to check string input format. The string can be in the format

single digit 0-9 followed by
single letter A-Z OR 07 OR 03 or AA followed by
two letters A-Z

Values ​​0AAA, 107ZF, 503GH, 0AAAA are valid. The line with which I create my Regex is as follows:

    "([0-9]{1})" +
    "((03$)|(07$)|(AA$)|[A-Z]{1})" +
    "([A-Z]{2})"

However, this does not confirm the lines in which the second member is one of 03, 07, or AA. During debugging, I removed the third member from the line used to build the regular expression, and found that the input lines of the form 103, 507, 6AA confirm ........

Any ideas why, when I then put the third term back in Regex, input strings like 1AAGM don't match?

Thanks tom

+5
source share
2 answers

, , 03, 07 AA ($ ). $ .

"^[0-9](03|07|AA|[A-Z])[A-Z]{2}$"
+9

, , "$" , ( ). , . Regex Buddy, :

([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

:

([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
+4

All Articles