Ищите Regex для проверки формата MM / DD / YY HH: mm - PullRequest
1 голос
/ 15 ноября 2011

Я ищу регулярное выражение для использования с элементом управления ASP.NET Regular Expression Validator для проверки следующего формата:

08/10/11 23:00

Это должно быть MM/DD/YY HH:mm с использованием 24-часовых часов. Заранее спасибо за любую помощь!

1 Ответ

4 голосов
/ 15 ноября 2011

Это соответствует вашей спецификации, хотя и немного гибко.

/^(?: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)
"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...