C#字符串检查

/// <summary>
        /// 检察是否都是数字
        /// </summary>
        /// <param name="str">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsNumeric(string str)
        {
            Regex reg = new Regex(@"^[+]?\d*$");
            return reg.IsMatch(str);
        }

        /// <summary>
        /// 检察是否正确的Email格式
        /// </summary>
        /// <param name="str">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsEmail(string str)
        {
            Regex reg = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            return reg.IsMatch(str);
        }

        /// <summary>
        /// 检察是否正确的日期格式
        /// </summary>
        /// <param name="str">要检查的字串</param>
        /// <returns>bool</returns>
        public static bool IsDate(string str)
        {
            //考虑到了4年一度的366天,还有特殊的2月的日期
            Regex reg = new Regex(@"^((((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$");
            return reg.IsMatch(str);
        }

猜你喜欢

转载自www.cnblogs.com/zengxh/p/12390751.html