【问题标题】:Regular expression to validate valid time验证有效时间的正则表达式
【发布时间】:2010-10-27 10:58:53
【问题描述】:

我需要一个正则表达式来验证时间。

有效值从0:0023:59

当时间小于10:00时也应该支持一个字符的数字。

这些是有效值:

  • 9:00
  • 09:00

【问题讨论】:

  • 对不起,我打错了,我希望第一个数字支持 1 个字符。即:2:00 和 02:00
  • 00:00, 01:00, ... 是有效值吗?
  • 您是否只需要正则表达式答案?
  • 用于正则表达式验证器

标签: c# .net regex


【解决方案1】:

试试这个正则表达式:

^(?:[01]?[0-9]|2[0-3]):[0-5][0-9]$

或者更明显:

^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

【讨论】:

    【解决方案2】:

    我不想窃取任何人的辛勤工作,但 this 显然正是您要找的。​​p>

    using System.Text.RegularExpressions;
    
    public bool IsValidTime(string thetime)
    {
        Regex checktime =
            new Regex(@"^(20|21|22|23|[01]d|d)(([:][0-5]d){1,2})$");
    
        return checktime.IsMatch(thetime);
    }
    

    【讨论】:

    • 我不明白为什么当它不起作用时会得到支持。这三个d 字符将只匹配文字字符d,因此对于"00:00""23:59" 返回false,但对于"0d:0d" 返回true。它们应该改为 \d[0-9] 以匹配任何十进制数字。该答案中缺少该博客条目的下一行是“用于控制时间的时间正则表达式^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$”。
    【解决方案3】:

    我只会使用 DateTime.TryParse()。

    DateTime time;
    string timeStr = "23:00"
    
    if(DateTime.TryParse(timeStr, out time))
    {
      /* use time or timeStr for your bidding */
    }
    

    【讨论】:

    • 用于客户端验证
    【解决方案4】:

    聚会很晚,但我创建了这个正则表达式,我发现它最适合 24H 格式(HH:mm:ss OR HH:mm OR H:mm:ss OR H:mm):

    private bool TimePatternValidation(string time)
        => new Regex(@"^(([0-1]?[0-9])|([2][0-3]))(:([0-5][0-9])){1,2}$").IsMatch(time);
    

    【讨论】:

    • 如您所述,这与问题未请求的HH:mm:ss 格式匹配,但与问题请求的H:mm 格式不匹配问题。
    • 你说得对,我修好了。现在它适用于每个时间模式 (HH:mm:ss | H:mm:ss | HH:mm | H:mm)
    【解决方案5】:

    正则表达式 ^(2[0-3]|[01]d)([:][0-5]d)$ 应该匹配 00:00 到 23:59。不懂C#所以不能给你相关代码。

    /RS

    【讨论】:

      【解决方案6】:

      如果您想允许 militarystandard 使用 AM 和 PM(可选且不敏感),那么您可能需要试试这个。

      ^(?:(?:0?[1-9]|1[0-2]):[0-5][0-9]\s?(?:[AP][Mm]?|[ap][m]?)?|(?:00?|1[3-9]|2[0-3]):[0-5][0-9])$ 
      

      【讨论】:

        【解决方案7】:
        [RegularExpression(@"^(0[1-9]|1[0-2]):[0-5][0-9]:[0-5][0-9] (am|pm|AM|PM)$", 
                           ErrorMessage = "Invalid Time.")]
        

        试试这个

        【讨论】:

        • 为什么 OP 应该“试试这个”? 好的答案将始终解释所做的工作以及这样做的原因,不仅适用于 OP,而且适用于可能会发现此问题并正在阅读您的答案的 SO 的未来访问者。
        【解决方案8】:

        更好!!!

            public bool esvalida_la_hora(string thetime)
            {
                Regex checktime = new Regex("^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$");
                if (!checktime.IsMatch(thetime))
                    return false;
        
                if (thetime.Trim().Length < 5)
                    thetime = thetime = "0" + thetime;
        
                string hh = thetime.Substring(0, 2);
                string mm = thetime.Substring(3, 2);
        
                int hh_i, mm_i;
                if ((int.TryParse(hh, out hh_i)) && (int.TryParse(mm, out mm_i)))
                {
                    if ((hh_i >= 0 && hh_i <= 23) && (mm_i >= 0 && mm_i <= 59))
                    {
                        return true;
                    }
                }
                return false;
            }
        

        【讨论】:

        • 为什么这样更好?这个答案似乎不符合问题的标准?
        【解决方案9】:
        public bool IsTimeString(string ts)
        {
            if (ts.Length == 5 && ts.Contains(':'))
            {
                int h;
                int m;
                return int.TryParse(ts.Substring(0, 2), out h) 
                                    && int.TryParse(ts.Substring(3, 2), out m) 
                                    && h >= 0 && h < 24 
                                    && m >= 0 && m < 60;
             }
             else
                 return false;
        }
        

        【讨论】:

        • 在您的回答中添加更多详细信息。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多