2012-06-14 37 views
0

我对正则表达式很陌生,所以我不知道从哪里开始。我需要在文件中存储时间范围值。我想在保存之前对照正则表达式进行验证。确保字符串是给定格式的正则表达式是什么?

的格式必须是:

00:00-00:00 

其中的数字的分钟值可以是0和作为

23:59-23:59 

如何确保字符串的最高值可以被表示匹配这个约束?

+1

你可以从[上正则表达式的维基百科页面]启动(http://en.wikipedia.org/wiki/Regular_expression)并从那里出发。 – lanzz

+0

正则表达式在[这里]简要解释(http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5&AspxAutoDetectCookieSupport=1)或尝试[this](http://forums.asp.net/t/884783。 ASPX/1) – hims056

回答

0

试试这个

\b((?:(?:[01][0-9]|2[0-3]):(?:[0-5][0-9]))-(?:(?:[01][0-9]|2[0-3]):(?:[0-5][0-9])))\b 

说明

\b     # Assert position at a word boundary 
(     # Match the regular expression below and capture its match into backreference number 1 
    (?:     # Match the regular expression below 
     (?:     # 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” 
      [0-9]    # Match a single character in the range between “0” and “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” 
     [0-9]    # Match a single character in the range between “0” and “9” 
    ) 
    ) 
    -     # Match the character “-” literally 
    (?:     # Match the regular expression below 
     (?:     # 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” 
      [0-9]    # Match a single character in the range between “0” and “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” 
     [0-9]    # Match a single character in the range between “0” and “9” 
    ) 
    ) 
) 
\b     # Assert position at a word boundary 
相关问题