C# 正则表达式大全

#region 正则表达式字符串

    ///

    /// 正则表达式字符串

    ///

    public class StrRegex

    {

        #region 正则表达式替换字符串

        ///

        /// 正则表达式替换字符串

        ///

        /// 字符串内容

        /// 替换字符

        /// 替换值

        ///

        public static string RegexReplace(string inputString, string pattern, string replaceStr)

        {

            try

            {

                return Regex.Replace(inputString, pattern, replaceStr);

            }

            catch (Exception e)

            {

                return e.Message;

            }

        }

        #endregion

        #region 判断字符串是否为正整数

        ///

        /// 判断字符串是否为正整数

        ///

        /// 要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool IsInt(String objString)

        {

            Regex myReg = new Regex(@"^\d+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region 判断输入的字符串是否全是英文(不区分大小写)

        ///

        /// 判断输入的字符串是否全是英文(不区分大小写)

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool isEnglishString(String objString)

        {

            Regex myReg = new Regex(@"^[a-zA-Z]+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        ///

        /// 返回字符串中的数字

        ///

        ///

        ///

        public static string RunNumber(string objString)

        {

            return Regex.Match(objString, "[0-9]+").Value.ToString();

        }

        ///

        /// 返回字符串中左边的字符

        ///

        ///

        ///

        public static string RunLeftString(string objString)

        {

            return Regex.Match(objString, "[%*/+ -.A-Za-z]+").Value.ToString();

        }

        ///

        /// 返回字符串中右边的字符

        ///

        ///

        ///

        public static string RunRightString(string objString)

        {

            return Regex.Match(objString, "[%*/+ -.A-Za-z]+$").Value.ToString();

        }

        ///

        /// 返回字符串中的字符

        ///

        ///

        ///

        public static string RunString(string objString)

        {

            return Regex.Match(objString, "[A-Za-z]+").Value.ToString();

        }

        #region 判断所输入的字符串是否为中文

        ///

        /// 判断所输入的字符串是否为中文

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool isChinese(String objString)

        {

            Regex myReg = new Regex(@"^[\u4e00-\u9fa5]+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region 判断输入字符串是否为英文及数字(英文不区分大小写)

        ///

        /// 判断输入字符串是否为英文及数字(英文不区分大小写)

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool isEngNum(String objString)

        {

            Regex myReg = new Regex(@"^[*/+-a-zA-Z0-9]+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region 判断输入字符串是否为英文A-D及数字(英文限制在A-D之间英文不区分大小写)

        ///

        /// 判断输入字符串是否为英文A-D及数字(英文限制在A-D之间英文不区分大小写)

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool isEngNumMax(String objString)

        {

            Regex myReg = new Regex(@"^[a-dA-D0-9]+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region  判断是否为英文及数字组合

        ///

        /// 判断是否为英文及数字组合

        ///

        ///

        ///

        public static bool InEngNum(string objString)

        {

            //Regex myReg = new Regex(@"^(?![0-9]+$)[a-zA-Z0-9]{1,25}$");          

            //return myReg.IsMatch(objString);"^[a-zA-Z]\w{5,17}$"

            return Regex.IsMatch(objString, @"^[*/+-a-zA-Z0-9]{1,20}$");

        }

        #endregion

        #region 判断输入字符串是否为英文,数字,中文(英文不区分大小写)

        ///

        /// 判断输入字符串是否为英文,数字,中文(英文不区分大小写)

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool isChineseEngNum(String objString)

        {

            Regex myReg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z0-9]+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region 判断输入字符串是否为小数

        ///

        /// 判断输入字符串是否为小数

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool isFloat(String objString)

        {

            Regex myReg = new Regex(@"^[0-9]+[.][0-9]+|[0-9]+$");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region 判断日期格式是否有效

        ///

        /// 判断日期格式是否有效

        ///

        ///

        ///

        public static bool IsDate(String objString)

        {

            Regex myReg = new Regex(@"\b(?\d{2,4})-(?\d{1,2})-(?\d{1,2})\b");

            return myReg.IsMatch(objString);

        }

        #endregion

        #region 判断字符串是否符合此正则表达试

        ///

        /// 判断字符串是否符合此正则表达试

        ///

        /// 所要匹配的字符串

        /// 正则字符串(如:^[1-9]{1}$)

        /// 返回真假值,true:匹配;false:不匹配

        public static bool IsFitStrings(String str, String regString)

        {

            Regex objPattern = new Regex(regString);

            bool returnValue = objPattern.IsMatch(str);

            return returnValue;

        }

        #endregion

        #region 判断字符串是否为手机号或小灵通号

        ///

        /// 判断字符串是否为手机号或小灵通号

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool IsMobile(string telNumber)

        {

            if (telNumber == "")

                return false;

            Regex myReg = new Regex(@"^((\d{11,12})|(\d{7}))$");

            return myReg.IsMatch(telNumber);

        }

        #endregion

        #region 判断字符串是否为Email地址

        ///

        /// 判断字符串是否为Email地址

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool IsEmail(string email)

        {

            if (email == "")

            {

                return false;

            }

            Regex myReg = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");

            return myReg.IsMatch(email);

        }

        #endregion

        #region 判断字符串是否为座机(如xxx-xxxxxxx-xxx)

        ///

        /// 判断字符串是否为座机(如xxx-xxxxxxx-xxx)

        ///

        /// 所要匹配的字符串

        /// 返回真假值,true:匹配;false:不匹配

        public static bool IsTel(string tel)

        {

            if (tel == "")

                return false;

            Regex myReg = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}(-\d{1,5})?$");

            return myReg.IsMatch(tel);

        }

        #endregion

        #region 判断是否为邮政编码

        ///

        /// 判断是否为邮政编码

        ///

        ///

        ///

        public static bool IsValidZip(string Zip)

        {

            return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$");

        }

        #endregion

        #region  判断是否为有效身份证号

        ///

        /// 判断是否为有效身份证号

        ///

        ///

        ///

        public static bool IsIdCard(string IdCard)

        {

            return Regex.IsMatch(IdCard, @"^\d{15}|\d{18}$");

        }

        #endregion

        #region 返回分割字符串

        ///

        /// 返回分割字符串

        ///

        /// 要分割的字符串集

        /// 指定分割字符

        ///

        public static string FindStr(string Str, string spliststr)

        {

            string[] str2 = System.Text.RegularExpressions.Regex.Split(Str, @"[" + spliststr + "]+");

            foreach (string i in str2)

            {

                return i.ToString();

            }

            return "";

        }

        #endregion

    }

    #endregion

你可能感兴趣的:(Winform,C#,c#,正则表达式)