对于12小时AM和PM时间的正则表达式 [英] time regex for 12hr am and pm

查看:1006
本文介绍了对于12小时AM和PM时间的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此正则表达式

  ^(00 | 0 [0-9] | 1 [012]): 0-5] [0-9]? ((A | P)M |(A | P)M)$ 



但问题是它在接受
00:00 AM这我不希望是这样的
怎么不理?
我想时间是在这种格式
上午12点12
它不应该在这个
上午12时21
我使用这个代码进行验证

 私人布尔va​​ltime()
{
正则表达式的regex ​​=新的正则表达式(^(00 | 0 0-9] | 1 [012]):[0-5] [0-9]((A | p)M |(A | p)M)$);
如果(regex.IsMatch(textBox1.Text))
{
返回FALSE;
}
,否则
{
返回真;
}
}


解决方案

您可以使用:

  Regex.IsMatch(输入,@^(0 [1-9] | 1 [0-2] ):[0-5] [0-9] [AP] M $,RegexOptions.IgnoreCase); 


I have made this regex

 ^(00|0[0-9]|1[012]):[0-5][0-9]? ((a|p)m|(A|P)M)$

but the problem is it is accepting 00:00 am which i dont want to be like this how to ignore? I want time to be in this format 12:12 am it should not be in this 00:21 am I am using this code for validation

        private bool valtime()
    {
        Regex regex = new Regex("^(00|0[0-9]|1[012]):[0-5][0-9]? ((a|p)m|(A|P)M)$");
        if (regex.IsMatch(textBox1.Text))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

解决方案

You could use:

Regex.IsMatch(input, @"^(0[1-9]|1[0-2]):[0-5][0-9] [ap]m$", RegexOptions.IgnoreCase);

这篇关于对于12小时AM和PM时间的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆