C#正则表达式判断输入日期格式是否正确
网络编程 2021-07-05 10:07www.168986.cn编程入门
本文给大家介绍使用C#判断输入日期格式是否正确的方法。本文给大家分享了两段代码,代码非常简单易懂,感兴趣的朋友一起看看吧
本文将介绍一段实例代码,来讲解利用正则表达式使C#判断输入日期格式是否正确的方法。希望这段代码能对大家有所帮助。
通常我们在用C#编写系统程序或者Web开发时,都会遇到需要验证输入的字符串是否是日期的情况,狼蚁网站SEO优化为大家介绍一种非常全面的用正则表达式验证日期的方法:
c 正则表达式日期代码一:
/// <summary> /// 是否为日期型字符串 /// </summary> /// <param name="StrSource">日期字符串(2008-05-08)</param> /// <returns></returns> 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-))$"); } /// <summary> /// 是否为时间型字符串 /// </summary> /// <param name="source">时间字符串(15:00:00)</param> /// <returns></returns> public static bool IsTime(string StrSource) { return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$"); } /// <summary> /// 是否为日期+时间型字符串 /// </summary> /// <param name="source"></param> /// <returns></returns> 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'((?<!(\\2((0?[2469])|11)\\2))31)|(?<!\\2(0?2)\\2)(29|30)|((?<=((1[6-9]|[2-9]\\d)(0[48]| [2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\\2\\3\\2)29)|((0?[1-9])|(1\\d)|(2[0- 8])))(?:(?=\\x20\\d)\\x20|$))?((?<time>((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; }
以上就是长沙网络推广给大家介绍的C#正则表达式判断输入日期格式是否正确的全部内容,希望对大家学习c正则表达式日期判断有所帮助。同时也非常感谢大家一直以来对狼蚁SEO网站的支持。
上一篇:MongoDB正则表达式及应用
下一篇:C#正则表达式之Regex类用法详解
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程