【问题标题】:Simple Regular Expression (Regex) issue (.net)简单正则表达式 (Regex) 问题 (.net)
【发布时间】:2012-04-03 12:14:36
【问题描述】:

我正在尝试使用 .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)不匹配吗?

谢谢 汤姆

【问题讨论】:

标签: c# .net regex


【解决方案1】:

这是因为您的表达式需要带有 0307AA 的字符串在此处结束($ 匹配输入的结尾)。从这些子表达式中删除$,并将其移至表达式的末尾。

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

【讨论】:

  • +1,而我的回答确实解释了他在做什么并提供了解决方案。例如,此答案确保 107ZFD 不会验证,而我的答案将匹配字符串的 107ZF 部分。不确定他想要什么,但这也是一个很好的答案。
【解决方案2】:

我相信这是因为您在正则表达式中使用了“$”,这意味着在这种情况下,在行尾(在字符串末尾或换行符之前)断言位置。删除它,它应该可以工作。来自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}»

【讨论】:

  • 同意,$ 应该是问题所在。
猜你喜欢
  • 2011-05-13
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 2013-04-01
相关资源
最近更新 更多