【问题标题】:Looking for Regex to validate MM/DD/YY HH:mm format寻找正则表达式来验证 MM/DD/YY HH:mm 格式
【发布时间】:2011-11-16 05:28:05
【问题描述】:

我正在寻找与 ASP.NET 正则表达式验证器控件一起使用的正则表达式,以检查以下格式:

08/10/11 23:00

它必须是MM/DD/YY HH:mm,使用 24 小时制。提前感谢您的帮助!

【问题讨论】:

  • 用户设置有什么作用吗?

标签: asp.net regex validation


【解决方案1】:

这符合您的规范,尽管有点灵活。

/^(?:0?[1-9]|1[012])/(?:0?[1-9]|[12]\d|3[01])/(?:\d\d)\s+(?:[01]?\d|2[0-3]):(?:[0-5]\d)$/

我猜你可以根据自己的需要定制它。

说明:

    "
^              # Assert position at the beginning of the string
(?:            # Match the regular expression below
                  # Match either the regular expression below (attempting the next alternative only if this one fails)
      0              # Match the character “0” literally
         ?              # Between zero and one times, as many times as possible, giving back as needed (greedy)
      [1-9]          # Match a single character in the range between “1” and “9”
   |              # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      1              # Match the character “1” literally
      [012]          # Match a single character present in the list “012”
)
/              # Match the character “/” literally
(?:            # Match the regular expression below
                  # Match either the regular expression below (attempting the next alternative only if this one fails)
      0              # Match the character “0” literally
         ?              # Between zero and one times, as many times as possible, giving back as needed (greedy)
      [1-9]          # Match a single character in the range between “1” and “9”
   |              # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      [12]           # Match a single character present in the list “12”
      \d             # Match a single digit 0..9
   |              # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      3              # Match the character “3” literally
      [01]           # Match a single character present in the list “01”
)
/              # Match the character “/” literally
(?:            # Match the regular expression below
   \d             # Match a single digit 0..9
   \d             # Match a single digit 0..9
)
\s             # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
   +              # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?:            # Match the regular expression below
                  # Match either the regular expression below (attempting the next alternative only if this one fails)
      [01]           # Match a single character present in the list “01”
         ?              # Between zero and one times, as many times as possible, giving back as needed (greedy)
      \d             # Match a single digit 0..9
   |              # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      2              # Match the character “2” literally
      [0-3]          # Match a single character in the range between “0” and “3”
)
:              # Match the character “:” literally
(?:            # Match the regular expression below
   [0-5]          # Match a single character in the range between “0” and “5”
   \d             # Match a single digit 0..9
)
$              # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

【讨论】:

  • 为什么您的括号分组以(?: 开头,而不仅仅是(?这两个额外的字符如何改变正则表达式逻辑?
  • @ean5533 因为我不想将它们用作反向引用,而是简单地使用交替并将正则表达式的各个部分分组。如果您删除 ?: 您将每个括号存储到一个反向引用组中,在这种情况下没有要求。逻辑保持不变。
  • 有趣,不知道那个修饰符。谢谢。
  • -1:not 匹配月份 10would 匹配月份 00,日期 00 - 有点太灵活了切合实际(尤其是不允许在 10 月约会)
  • @CodeJockey 我没想到你会做得更少。感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 2015-09-03
  • 1970-01-01
相关资源
最近更新 更多