简单的正则表达式(Regex)问题 (.net)

5

我正在尝试使用.NET正则表达式验证字符串的输入格式。该字符串可以是以下格式之一

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这样的输入字符串就无法匹配?

谢谢 汤姆


顺便说一下,我发现这个工具非常有用,可以测试正则表达式:http://gskinner.com/RegExr/ - michele
2个回答

9
这是因为您的表达式需要以0307AA字符串结束($匹配输入的末尾)。从这些子表达式中删除$,并将其移到表达式的末尾。
"^[0-9](03|07|AA|[A-Z])[A-Z]{2}$"

虽然我的回答解释了他在做什么并提供了解决方案,但是+1。这个答案确保例如107ZFD不会被验证,而我的答案将匹配字符串的107ZF部分。不确定他想要哪一个,但这也是一个好答案。 - Matt

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 “0and9” «[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

同意,$符号应该是问题所在。 - jessehouwing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接