当前位置:网站首页>Regular expression string

Regular expression string

2022-07-07 15:53:00 Le_ Sam

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace tablegen2.common
{
    #region  Regular expression string 
    /// <summary>
    ///  Regular expression string 
    /// </summary>
    public class StrRegex
    {
        #region  Regular expressions replace strings 
        /// <summary>
        ///  Regular expressions replace strings 
        /// </summary>
        /// <param name="inputString"> String content </param>
        /// <param name="pattern"> Replace character </param>
        /// <param name="replaceStr"> Replacement value </param>
        /// <returns></returns>
        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  Determine whether the string is a positive integer 
        /// <summary>
        ///  Determine whether the string is a positive integer 
        /// </summary>
        /// <param name="objString"> String to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool IsInt(String objString)
        {
            Regex myReg = new Regex(@"^\d+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region  Judge whether the input string is all in English ( Case insensitive )
        /// <summary>
        ///  Judge whether the input string is all in English ( Case insensitive )
        /// </summary>
        /// <param name="objString"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool isEnglishString(String objString)
        {
            Regex myReg = new Regex(@"^[a-zA-Z]+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        /// <summary>
        ///  Returns the number in a string 
        /// </summary>
        /// <param name="objString"></param>
        /// <returns></returns>
        public static string RunNumber(string objString)
        {
            return Regex.Match(objString, "[0-9]+").Value.ToString();
        }

        /// <summary>
        ///  Returns the left character in the string 
        /// </summary>
        /// <param name="objString"></param>
        /// <returns></returns>
        public static string RunLeftString(string objString)
        {
            return Regex.Match(objString, "[%*/+ -.A-Za-z]+").Value.ToString();
        }

        /// <summary>
        ///  Returns the right character in the string 
        /// </summary>
        /// <param name="objString"></param>
        /// <returns></returns>
        public static string RunRightString(string objString)
        {
            return Regex.Match(objString, "[%*/+ -.A-Za-z]+$").Value.ToString();
        }

        /// <summary>
        ///  Returns the characters in a string 
        /// </summary>
        /// <param name="objString"></param>
        /// <returns></returns>
        public static string RunString(string objString)
        {
            return Regex.Match(objString, "[A-Za-z]+").Value.ToString();
        }

        #region  Judge whether the input string is Chinese 
        /// <summary>
        ///  Judge whether the input string is Chinese 
        /// </summary>
        /// <param name="objString"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool isChinese(String objString)
        {
            Regex myReg = new Regex(@"^[\u4e00-\u9fa5]+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region  Judge whether the input string is in English and numbers ( English is not case sensitive )
        /// <summary>
        ///  Judge whether the input string is in English and numbers ( English is not case sensitive )
        /// </summary>
        /// <param name="objString"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool isEngNum(String objString)
        {
            Regex myReg = new Regex(@"^[*/+-a-zA-Z0-9]+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region  Judge whether the input string is in English A-D And numbers ( English is limited to A-D English is not case sensitive )
        /// <summary>
        ///  Judge whether the input string is in English A-D And numbers ( English is limited to A-D English is not case sensitive )
        /// </summary>
        /// <param name="objString"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool isEngNumMax(String objString)
        {
            Regex myReg = new Regex(@"^[a-dA-D0-9]+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region   Judge whether it is a combination of English and numbers 
        /// <summary>
        ///  Judge whether it is a combination of English and numbers 
        /// </summary>
        /// <param name="objString"></param>
        /// <returns></returns>
        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  Judge whether the input string is in English , Numbers , chinese ( English is not case sensitive )
        /// <summary>
        ///  Judge whether the input string is in English , Numbers , chinese ( English is not case sensitive )
        /// </summary>
        /// <param name="objString"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool isChineseEngNum(String objString)
        {
            Regex myReg = new Regex(@"^[\u4e00-\u9fa5a-zA-Z0-9]+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region  Judge whether the input string is decimal 
        /// <summary>
        ///  Judge whether the input string is decimal 
        /// </summary>
        /// <param name="objString"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool isFloat(String objString)
        {
            Regex myReg = new Regex(@"^[0-9]+[.][0-9]+|[0-9]+$");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region  Judge whether the date format is valid 
        /// <summary>
        ///  Judge whether the date format is valid 
        /// </summary>
        /// <param name="objString"></param>
        /// <returns></returns>
        public static bool IsDate(String objString)
        {
            Regex myReg = new Regex(@"\b(?<year>\d{2,4})-(?<month>\d{1,2})-(?<day>\d{1,2})\b");
            return myReg.IsMatch(objString);
        }
        #endregion

        #region  Judge whether the string conforms to this regular expression 
        /// <summary>
        ///  Judge whether the string conforms to this regular expression 
        /// </summary>
        /// <param name="str"> The string to match </param>
        /// <param name="regString"> Regular string ( Such as :^[1-9]{1}$)</param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        public static bool IsFitStrings(String str, String regString)
        {
            Regex objPattern = new Regex(regString);
            bool returnValue = objPattern.IsMatch(str);
            return returnValue;
        }
        #endregion

        #region  Judge whether the string is a mobile number or a small smart number 
        /// <summary>
        ///  Judge whether the string is a mobile number or a small smart number 
        /// </summary>
        /// <param name="telNumber"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        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  Determine if the string is Email Address 
        /// <summary>
        ///  Determine if the string is Email Address 
        /// </summary>
        /// <param name="email"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        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  Judge whether the string is a landline ( Such as xxx-xxxxxxx-xxx)
        /// <summary>
        ///  Judge whether the string is a landline ( Such as xxx-xxxxxxx-xxx)
        /// </summary>
        /// <param name="tel"> The string to match </param>
        /// <returns> Return true and false value ,true: matching ;false: Mismatch </returns>
        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  Determine whether it is a postal code 
        /// <summary>
        ///  Determine whether it is a postal code 
        /// </summary>
        /// <param name="Zip"></param>
        /// <returns></returns>
        public static bool IsValidZip(string Zip)
        {
            return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$");
        }
        #endregion

        #region   Determine whether it is a valid ID number 
        /// <summary>
        ///  Determine whether it is a valid ID number 
        /// </summary>
        /// <param name="IdCard"></param>
        /// <returns></returns>
        public static bool IsIdCard(string IdCard)
        {
            return Regex.IsMatch(IdCard, @"^\d{15}|\d{18}$");
        }
        #endregion

        #region  Returns the split string 
        /// <summary>
        ///  Returns the split string 
        /// </summary>
        /// <param name="Str"> The set of strings to split </param>
        /// <param name="spliststr"> Specify split character </param>
        /// <returns></returns>
        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

}

 

原网站

版权声明
本文为[Le_ Sam]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130609270721.html