/*
    * 版权所有：Copyright (C) 2013,加一信息科技
　　* Version: 1.0
　　* 创建日期:  2013-05-16 
　　* 作者: 张琨
　　* 说明: 常用正则验证

　　* 修改日期: yyyy-MM-dd
　　* 修改者: 修改者
　　* 修改说明: 修改说明
*/
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace ZXDT.Common
{
    /// <summary>
    /// 提供常用正则验证
    /// </summary>
    public class RegexUtil
    {
        /// <summary>
        /// 是否为纯数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumber(string str)
        {
            return Regex.IsMatch(str, @"^\d+$");
        }
        /// <summary>
        /// 是否为纯字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsLetter(string str)
        {
            return Regex.IsMatch(str, @"^[a-zA-Z]+$");
        }
        /// <summary>
        /// 是否为图片格式
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsImage(string filename)
        {
            return Regex.IsMatch(filename, @"(?i)\.(jpg|gif|png|bmp|tiff)$");
        }
        /// <summary>
        /// 验证是否为用户名格式（字母、数字、下划线、汉字）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUsername(string str)
        {
            return Regex.IsMatch(str, @"^\w+$");
        }

        public static bool IsUrl(string str)
        {
            return Regex.IsMatch(str, @"(?i)^http(?s)://([\w\-]+\.)+[\w\-]+(/[\w\-./?%&=]*)?$");
        }

        public static bool IsEmail(string str)
        {
            return Regex.IsMatch(str, @"(?i)^([a-z0-9][a-z0-9_\-\.]+)@([a-z0-9][a-z0-9\.\-]{0,20})\.([a-z]{2,4})$");
        }

        public static bool IsIpAddress(string str)
        {
            return Regex.IsMatch(str, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
        }
        /// <summary>
        /// 验证是否为身份证号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIDCardNumber(string str)
        {
            if (str.Length == 15)
            {
                return Regex.IsMatch(str, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
            }
            else if (str.Length == 18)
            {
                return Regex.IsMatch(str, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$");
            }
            return false;
        }
        /// <summary>
        /// 是否为手机号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMobile(string str)
        {
            return Regex.IsMatch(str, @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$");
        }
        /// <summary>
        /// 是否为电话号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTelphone(string str)
        {
            string patt = @"^((\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})(-|\s)(\d{1,4})|(\d{7,8})(-|\s)(\d{1,4}))$";
            return Regex.IsMatch(str, patt);
        }
        /// <summary>
        /// 是否为日期格式（不含时间）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDate(string str)
        {
            string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))$";
            return Regex.IsMatch(str, patt);
        }
        /// <summary>
        /// 是否为时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTime(string str)
        {
            return Regex.IsMatch(str, @"^([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$");
        }
        /// <summary>
        /// 是否为日期时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string str)
        {
            string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))\s+([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$";
            return Regex.IsMatch(str, patt); 
        }

        /// <summary>
        /// 验证是否为用户名格式（字母、数字、下划线）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUserName(string str)
        {
            return Regex.IsMatch(str, @"^[0-9a-zA-Z_]+$");
        }
        /// <summary>
        /// 验证是否为字母数字格式（字母、数字）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsOnlyLetterNumber(string str)
        {
            return Regex.IsMatch(str, @"^[0-9a-zA-Z]+$");
        }

        /// <summary>
        /// 是否为手机号或电话
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMobileOrTel(string str)
        {
            return Regex.IsMatch(str, @"^((\d{3,4}-\d{7,8})|(\d{11}))$");
        }
        /// <summary>
        /// 是否为11位数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMobile1(string str)
        {
            return Regex.IsMatch(str, @"^(\d{11})$");
        }
        /// <summary>
        /// 是否为ip
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");

        }
    }
}
