我正在尝试使用.NET Regex来验证字符串的输入格式。字符串的格式可以是
single digit 0-9 followed by
single letter A-Z OR 07 OR 03 or AA followed by
two letters A-Z
所以0AAA,107ZF,503GH,0AAAA都是有效的。我构造我的正则表达式的字符串如下:
"([0-9]{1})" +
"((03$)|(07$)|(AA$)|[A-Z]{1})" +
"([A-Z]{2})"
然而,这并不验证第二项是03,07或AA之一的字符串。在调试时,我从用于构造正则表达式的字符串中删除了第三个术语,并发现103,507,6AA形式的输入字符串将验证.......
任何想法为什么,当我把第三个术语放回到正则表达式时,输入字符串如1AAGM不匹配?
由于 汤姆
答案 0 :(得分:9)
这是因为您的表达式要求03
,07
和AA
的字符串在那里结束($
匹配输入的结尾)。从这些子表达式中删除$
,并将其移动到表达式的末尾。
"^[0-9](03|07|AA|[A-Z])[A-Z]{2}$"
答案 1 :(得分:4)
我相信这是因为你在正则表达式中使用“$”,这意味着在这种情况下断言一行的结尾(在字符串的结尾或换行符之前)。删除它,它应该工作。从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}»