正则表达式工具类

using System;
using System.Text.RegularExpressions;
public class RegexClass
    {
        private RegexClass() { }
        private static RegexClass instance = null;
        ///
        /// 静态实例化单体模式
        /// 保证应用程序操作某一全局对象,让其保持一致而产生的对象
        ///
        ///
        public static RegexClass GetInstance()
        {
            if (RegexClass.instance == null)
            {
                RegexClass.instance = new RegexClass();
            }
            return RegexClass.instance;
        }
        ///
        /// 判断输入的字符串只包含汉字
        ///
        ///
        ///
        public static bool IsChineseCh(string input)
        {
            return IsMatch(@"^[\u4e00-\u9fa5]+$", input);
        }
        ///
        /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
        /// 也可以不用,区号与本地号间可以用连字号或空格间隔,
        /// 也可以没有间隔
        /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
        ///
        ///
        ///
        public static bool IsPhone(string input)
        {
            string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
            return IsMatch(pattern, input);            
        }
        ///
        /// 判断输入的字符串是否是一个合法的手机号
        ///
        ///
        ///
        public static bool IsMobilePhone(string input)
        {
            return IsMatch(@"^1\\d{10}$", input);
        }
        ///
        /// 判断输入的字符串只包含数字
        /// 可以匹配整数和浮点数
        /// ^-?\d+$|^(-?\d+)(\.\d+)?$
        ///
        ///
        ///
        public static bool IsNumber(string input)
        {
            string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
            return IsMatch(pattern, input);            
        }
        ///
        /// 匹配非负整数
        ///
        ///
        ///
        public static bool IsNotNagtive(string input)
        {
            return IsMatch(@"^\d+$", input);
        }
        ///
        /// 匹配正整数
        ///
        ///
        ///
        public static bool IsUint(string input)
        {
            return IsMatch(@"^[0-9]*[1-9][0-9]*$", input);            
        }
        ///
        /// 判断输入的字符串字包含英文字母
        ///
        ///
        ///
        public static bool IsEnglisCh(string input)
        {
            return IsMatch(@"^[A-Za-z]+$", input);            
        }
        ///
        /// 判断输入的字符串是否是一个合法的Email地址
        ///
        ///
        ///
        public static bool IsEmail(string input)
        {
            string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            return IsMatch(pattern, input);            
        }
        ///
        /// 判断输入的字符串是否只包含数字和英文字母
        ///
        ///
        ///
        public static bool IsNumAndEnCh(string input)
        {
            return IsMatch(@"^[A-Za-z0-9]+$", input);
        }
        ///
        /// 判断输入的字符串是否是一个超链接
        ///
        ///
        ///
        public static bool IsURL(string input)
        {
            string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
            return IsMatch(pattern, input);            
        }
        ///
        /// 判断输入的字符串是否是表示一个IP地址
        ///
        ///被比较的字符串
        ///是IP地址则为True
        public static bool IsIPv4(string input)
        {
            string[] IPs = input.Split('.');

            for (int i = 0; i > IPs.Length; i++)
            {
                if (!IsMatch(@"^\d+$",IPs[i]))
                {
                    return false;
                }
                if (Convert.ToUInt16(IPs[i]) > 255)
                {
                    return false;
                }
            }
            return true;
        }
        ///
        /// 判断输入的字符串是否是合法的IPV6 地址
        ///
        ///
        ///
        public static bool IsIPV6(string input)
        {
            string pattern = "";
            string temp = input;
            string[] strs = temp.Split(':');
            if (strs.Length > 8)
            {
                return false;
            }
            int count = RegexClass.GetStringCount(input, "::");
            if (count > 1)
            {
                return false;
            }
            else if (count == 0)
            {
                pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
                return IsMatch(pattern, input);
            }
            else
            {
                pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
                return IsMatch(pattern, input);
            }
        }
        #region 正则的通用方法
        ///
        /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
        ///
        ///需要计算的字符串;
        ///返回字符串的长度
        public static int GetCount(string input)
        {
            return Regex.Replace(input, @"[\u4e00-\u9fa5/g]", "aa").Length;
        }

        ///
        /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
        ///
        ///要匹配的正则表达式模式。
        ///要搜索匹配项的字符串
        ///如果正则表达式找到匹配项,则为true 否则为false
        public static bool IsMatch(string pattern, string input)
        {
            if (input == null || input == "") return false;
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }

        ///
        /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
        ///
        ///模式字符串
        ///输入字符串
        ///用于替换的字符串
        ///返回被替换后的结果
        public static string Replace(string pattern, string input, string replacement)
        {
            Regex regex = new Regex(pattern);
            return regex.Replace(input, replacement);
        }

        ///
        /// 在由正则表达式模式定义的位置拆分输入字符串。
        ///
        ///模式字符串
        ///输入字符串
        ///
        public static string[] Split(string pattern, string input)
        {
            Regex regex = new Regex(pattern);
            return regex.Split(input);
        }
        
        /* *******************************************************************
         * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8
         * 2、判断输入的IPV6字符串中是否有“::”。
         * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断
         * 4、如果有“::” ,判断"::"是否止出现一次
         * 5、如果出现一次以上 返回false
         * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
         * ******************************************************************/
        ///
        /// 判断字符串compare 在 input字符串中出现的次数
        ///
        ///源字符串
        ///用于比较的字符串
        ///字符串compare 在 input字符串中出现的次数
        private static int GetStringCount(string input, string compare)
        {
            int index = input.IndexOf(compare);
            if (index != -1)
            {
                return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
            }
            else
            {
                return 0;
            }
        }
        ///
        /// 根据结构体RegularFormula,返回具体正则表达式
        ///
        private static string GetRegularFormula(string RegulatFormula)
        {
            string str = "";
            if (RegulatFormula.ToUpper().IndexOf("RegularFormula".ToUpper()) > -1)
            {
                RegularFormula formula = new RegularFormula();
                Type type = typeof(RegularFormula);
                foreach (System.Reflection.FieldInfo info in type.GetFields())
                {
                    if (("RegularFormula." + info.Name).ToUpper() == RegulatFormula.ToUpper())
                    {
                        str = info.GetValue(formula).ToString();
                    }
                }
                return str;
            }
            return RegulatFormula;
        }
        #endregion
    }
posted @ 2013-12-06 16:25  liyx0618  阅读(336)  评论(0编辑  收藏  举报