zoukankan      html  css  js  c++  java
  • 验证时间的正则表达式

    验证日期,时间,日期时间

            /// <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]|1d|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]|1d|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)$ ");
            } 

    时间相关的正则表达式

        //是否是整数
        public static bool IsInt(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^[0-9]*$");
        }
    
    yyyy/MM/dd格式
    
    ^(?<year>\d{2,4})/(?<month>\d{1,2})/(?<day>\d{1,2})$
    
    
    yyyy-MM-dd格式
    
    ^(?<year>\d{2,4})-(?<month>\d{1,2})-(?<day>\d{1,2})$
    
    
    yyyy.MM.dd 格式
    
    ^(?<year>\d{2,4})[.](?<month>\d{1,2})[.](?<day>\d{1,2})$
    
    
    yyyy年MM月dd日格式  (可以不包含年和日)
    
    ^((?<year>\d{2,4})年)?(?<month>\d{1,2})月((?<day>\d{1,2})日)?$
    
    
    yyyy年MM月dd日格式(MM、dd为中文数字)
    
    ^((?<year>\d{2,4})年)?(正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}日)?$
    
    
    yyyy年MM月dd日格式(年月日均为中文数字)
    
    ^(零|〇|一|二|三|四|五|六|七|八|九|十){2,4}年((正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}(日)?)?)?$
    
    
    农历日期(年月日,可不包含日,匹配不含“初X”的日期)
    
    ^(甲|乙|丙|丁|戊|己|庚|辛|壬|癸)(子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥)年((正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}(日)?)?)?$
    
    
    农历日期(年月日,可不包含年,匹配“初X”类的日期)
    
    ^((甲|乙|丙|丁|戊|己|庚|辛|壬|癸)(子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥)年)?(正|一|二|三|四|五|六|七|八|九|十|十一|十二)月初(一|二|三|四|五|六|七|八|九|十)$
    
    
    
    XX时XX分XX秒格式(可不含秒)
    
    ^(?<hour>\d{1,2})(时|点)(?<minute>\d{1,2})分((?<second>\d{1,2})秒)?$
    
    
    XX时XX分XX秒格式(中文数字,可不含秒)
    
    ^((零|一|二|三|四|五|六|七|八|九|十){1,3})(时|点)((零|一|二|三|四|五|六|七|八|九|十){1,3})分(((零|一|二|三|四|五|六|七|八|九|十){1,3})秒)?$
    
    
    XX分XX秒格式(中文数字)
    
    ^((零|一|二|三|四|五|六|七|八|九|十){1,3})分((零|一|二|三|四|五|六|七|八|九|十){1,3})秒$
    
    
    XX时格式
    
    \b(?<hour>\d{1,2})(时|点钟)\b

    测试

        /// <summary>  
        /// 使用正则表达式判断是否为日期  
        /// </summary>  
        /// <param name="str" type=string></param>  
        /// <returns name="isDateTime" type=bool></returns>  
        public bool IsDateTime(string str)  
        {  
            bool isDateTime = false;  
            // yyyy/MM/dd  
            if (Regex.IsMatch(str, "^(?<year>\d{2,4})/(?<month>\d{1,2})/(?<day>\d{1,2})$"))  
                isDateTime = true;  
            // yyyy-MM-dd   
            else if (Regex.IsMatch(str, "^(?<year>\d{2,4})-(?<month>\d{1,2})-(?<day>\d{1,2})$"))  
                isDateTime = true;  
            // yyyy.MM.dd   
            else if (Regex.IsMatch(str, "^(?<year>\d{2,4})[.](?<month>\d{1,2})[.](?<day>\d{1,2})$"))  
                isDateTime = true;  
            // yyyy年MM月dd日  
            else if (Regex.IsMatch(str, "^((?<year>\d{2,4})年)?(?<month>\d{1,2})月((?<day>\d{1,2})日)?$"))  
                isDateTime = true;  
            // yyyy年MM月dd日  
            else if (Regex.IsMatch(str, "^((?<year>\d{2,4})年)?(正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}日)?$"))  
                isDateTime = true;  
          
            // yyyy年MM月dd日  
            else if (Regex.IsMatch(str, "^(零|〇|一|二|三|四|五|六|七|八|九|十){2,4}年((正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}(日)?)?)?$"))  
                isDateTime = true;  
            // yyyy年  
            //else if (Regex.IsMatch(str, "^(?<year>\d{2,4})年$"))  
            //    isDateTime = true;  
          
            // 农历1  
            else if (Regex.IsMatch(str, "^(甲|乙|丙|丁|戊|己|庚|辛|壬|癸)(子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥)年((正|一|二|三|四|五|六|七|八|九|十|十一|十二)月((一|二|三|四|五|六|七|八|九|十){1,3}(日)?)?)?$"))  
                isDateTime = true;  
            // 农历2  
            else if (Regex.IsMatch(str, "^((甲|乙|丙|丁|戊|己|庚|辛|壬|癸)(子|丑|寅|卯|辰|巳|午|未|申|酉|戌|亥)年)?(正|一|二|三|四|五|六|七|八|九|十|十一|十二)月初(一|二|三|四|五|六|七|八|九|十)$"))  
                isDateTime = true;  
          
            // XX时XX分XX秒  
            else if (Regex.IsMatch(str, "^(?<hour>\d{1,2})(时|点)(?<minute>\d{1,2})分((?<second>\d{1,2})秒)?$"))  
                isDateTime = true;  
            // XX时XX分XX秒  
            else if (Regex.IsMatch(str, "^((零|一|二|三|四|五|六|七|八|九|十){1,3})(时|点)((零|一|二|三|四|五|六|七|八|九|十){1,3})分(((零|一|二|三|四|五|六|七|八|九|十){1,3})秒)?$"))  
                isDateTime = true;  
            // XX分XX秒  
            else if (Regex.IsMatch(str, "^(?<minute>\d{1,2})分(?<second>\d{1,2})秒$"))  
                isDateTime = true;  
            // XX分XX秒  
            else if (Regex.IsMatch(str, "^((零|一|二|三|四|五|六|七|八|九|十){1,3})分((零|一|二|三|四|五|六|七|八|九|十){1,3})秒$"))  
                isDateTime = true;  
          
            // XX时  
            else if (Regex.IsMatch(str, "\b(?<hour>\d{1,2})(时|点钟)\b"))  
                isDateTime = true;  
            else  
                isDateTime = false;  
          
            return isDateTime;  
        }  
    View Code
  • 相关阅读:
    CodeForces 7B
    CodeForces 4D
    离散化
    线段树入门
    洛谷 P3951 小凯的疑惑(赛瓦维斯特定理)
    Codeforces 1295D Same GCDs (欧拉函数)
    Codeforces 1295C Obtain The String (二分)
    Codeforces 1295B Infinite Prefixes
    Codeforces 1295A Display The Number(思维)
    Codeforces 1294F Three Paths on a Tree(树的直径,思维)
  • 原文地址:https://www.cnblogs.com/bit-by-bit/p/6703355.html
Copyright © 2011-2022 走看看