千家信息网

C#中怎么使用正则表达式判断输入日期格式是否正确

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,C#中怎么使用正则表达式判断输入日期格式是否正确,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。c 正则表达式日期代码一:
千家信息网最后更新 2025年02月08日C#中怎么使用正则表达式判断输入日期格式是否正确

C#中怎么使用正则表达式判断输入日期格式是否正确,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

c 正则表达式日期代码一:

///     /// 是否为日期型字符串    ///     /// 日期字符串(2008-05-08)    ///     public static bool IsDate(string StrSource)    {      return Regex.IsMatch(StrSource, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");    }    ///     /// 是否为时间型字符串    ///     /// 时间字符串(15:00:00)    ///     public static bool IsTime(string StrSource)    {      return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");    }    ///     /// 是否为日期+时间型字符串    ///     ///     ///     public static bool IsDateTime(string StrSource)    {      return Regex.IsMatch(StrSource, @"^(((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$ ");    }

c 正则表达式日期代码二:

//是否是整数  public static bool IsInt(string StrSource)  {    return Regex.IsMatch(StrSource, @"^[0-9]*$");  }  public static bool IsDate(string strDate)  {    if (string.IsNullOrEmpty(strDate))      return false;    string s_reg = @"^(?ni:(?=\\d)((?'year'((1[6-9])|([2-9]\\d))\\d\\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\\2(?'day'((?((0?[1-9]|1[012])(:[0-5]?\\d){0,2}(\\x20[AP]M))|([01]?\\d|2[0-3])(:[0-5]?\\d){1,2}))?)$";    Regex reg = new Regex(s_reg);    if (reg.IsMatch(strDate.ToLower()))       return true;    else       return false;   }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

0