寻找正则表达式验证MM / DD / YY HH:mm格式

时间:2011-11-15 19:09:00

标签: asp.net regex validation

我正在寻找一个与ASP.NET Regular Expression Validator控件一起使用的正则表达式来检查以下格式:

08/10/11 23:00

必须是MM/DD/YY HH:mm使用24小时制。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

这符合您的规格,虽然有点灵活。

/^(?: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)
"