SoFunction
Updated on 2025-03-01

Complete collection of various regular expression verification methods in C#

1. Verify the email address

        #region Verify email        /// <summary>
        /// Verify the email address        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsEmail(string source)
        {
            return (source, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", );
        }
        public static bool HasEmail(string source)
        {
            return (source, @"[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})", );
        }
        #endregion

2. Verification date

        #region Verification Date        /// <summary>
        /// Verification date        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsDateTime(string source)
        {
            try
            {
                DateTime time = (source);
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

3. Verify the URL

        #region Verify URL        /// <summary>
        /// Verify the URL        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsUrl(string source)
        {
            return (source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$", );
        }
        public static bool HasUrl(string source)
        {
            return (source, @"(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?", );
        }
        #endregion

4. Verify your mobile phone number

        #region Verify mobile phone number        /// <summary>
        /// Verify mobile phone number        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsMobile(string source)
        {
            return (source, @"^1[3456789]\d{9}$", );
        }
        public static bool HasMobile(string source)
        {
            return (source, @"1[3456789]\d{9}", );
        }
        #endregion

5. Verify whether the ID card is valid

        #region Verify that the ID card is valid        /// <summary>
        /// Verify that the ID card is valid        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool IsIDCard(string Id)
        {
            if ( == 18)
            {
                bool check = IsIDCard18(Id);
                return check;
            }
            else if ( == 15)
            {
                bool check = IsIDCard15(Id);
                return check;
            }
            else
            {
                return false;
            }
        }
        public static bool IsIDCard18(string Id)
        {
            long n = 0;
            if (((17), out n) == false || n < (10, 16) || (('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//Digital verification            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (((2)) == -1)
            {
                return false;//Provincial verification            }
            string birth = (6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if ((birth, out time) == false)
            {
                return false;//Birthday verification            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = (17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += (Wi[i]) * (Ai[i].ToString());
            }
            int y = -1;
            (sum, 11, out y);
            if (arrVarifyCode[y] != (17, 1).ToLower())
            {
                return false;//Check code verification            }
            return true;//Complied with GB11643-1999 standard        }
        public static bool IsIDCard15(string Id)
        {
            long n = 0;
            if ((Id, out n) == false || n < (10, 14))
            {
                return false;//Digital verification            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (((2)) == -1)
            {
                return false;//Provincial verification            }
            string birth = (6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if ((birth, out time) == false)
            {
                return false;//Birthday verification            }
            return true;//Complied with the 15-digit ID card standard        }
        #endregion

6. Verify IP address

    #region Verify IP        /// <summary>
        /// Verify IP        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsIP(string source)
        {
            return (source, @"^(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])$", );
        }
        public static bool HasIP(string source)
        {
            return (source, @"(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])", );
        }
        public static bool IsIp(string ip)
        {
            bool result = false;
            try
            {
                string[] iparg = ('.');
                if ( != ip &&  < 16 &&  == 4)
                {
                    int intip;
                    for (int i = 0; i < 4; i++)
                    {
                        intip = Convert.ToInt16(iparg[i]);
                        if (intip > 255)
                        {
                            result = false;
                            return result;
                        }
                    }
                    result = true;
                }
            }
            catch
            {
                return result;
            }
            return result;
        }
        #endregion

7. Verify whether the number is Int type

        Is #region Int type?        /// <summary>
        /// Is it Int type        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsInt(string source)
        {
            Regex regex = new Regex(@"^(-){0,1}\d+$");
            if ((source).Success)
            {
                if (((source) > 0x7fffffffL) || ((source) < -2147483648L))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        #endregion

8. Verify whether it is a Chinese phone number

        #region Is it a Chinese phone number? Format 010-85849685        /// <summary>
        /// Is it a Chinese phone number? Format 010-85849685        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsTel(string source)
        {
            return (source, @"^\d{3,4}-?\d{6,8}$", );
        }
        #endregion

9. Verify whether the length of the string is between the limited numbers. One Chinese is two characters

        #region Check whether the length of the string is between the limited numbers. One Chinese is two characters        /// <summary>
        /// See if the length of the string is between the limited numbers. One Chinese is two characters        /// </summary>
        /// <param name="source">String</param>        /// <param name="begin">greater than or equal to</param>        /// <param name="end">less than or equal to</param>        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsLengthStr(string source, int begin, int end)
        {
            int length = (source, @"[^\x00-\xff]", "OK").Length;
            if ((length &lt;= begin) &amp;&amp; (length &gt;= end))
            {
                return false;
            }
            return true;
        }
        #endregion

10. Verify the postal code

        #region Postal Code 6 Numbers        /// &lt;summary&gt;
        /// Postal code 6 numbers        /// &lt;/summary&gt;
        /// &lt;param name="source"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsPostCode(string source)
        {
            return (source, @"^\d{6}$", );
        }
        #endregion

11. Verify whether it is Chinese

        #region Chinese        /// &lt;summary&gt;
        /// Chinese        /// &lt;/summary&gt;
        /// &lt;param name="source"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsChinese(string source)
        {
            return (source, @"^[\u4e00-\u9fa5]+$", );
        }
        public static bool hasChinese(string source)
        {
            return (source, @"[\u4e00-\u9fa5]+", );
        }
        #endregion

12. Verify whether it is normal characters. Combination of letters, numbers, and underscores

        #region Verify whether it is a normal character. A combination of letters, numbers, and underscores        /// &lt;summary&gt;
        /// Verify that it is a normal character combination of letters, numbers, and underscores        /// &lt;/summary&gt;
        /// &lt;param name="source"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsNormalChar(string source)
        {
            return (source, @"[\w\d_]+", );
        }
        #endregion

13. Verify username: It must start with a letter and can contain letters, numbers, "_", ".", at least 5 characters

        #region Verification username: Must start with a letter and can contain letters, numbers, "_", ".", at least 5 characters        /// &lt;summary&gt;
        /// Verify username: Must start with a letter and can contain letters, numbers, "_", ".", at least 5 characters        /// &lt;/summary&gt;
        /// &lt;param name="str"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool checkUserId(string str)
        {
            Regex regex = new Regex("[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}");
            if ((str).Success)
                if ((str)[0]. == )
                    return true;
            return false;
        }
        #endregion

14. Verify whether it is a decimal

        #region Verify whether it is a decimal        bool IsValidDecimal(string strIn)
        {
            return (strIn, @"[0].d{1,2}|[1]");
        }
        #endregion

15. Verification year, month, date

        #region Verification date        bool IsValidDate(string strIn)
        {
            return (strIn, @"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$");
        }
        #endregion

16. Verify file suffix name

        #region Verify suffix name        bool IsValidPostfix(string strIn)
        {
            return (strIn, @".(?i:gif|jpg)$");
        }
        #endregion

17. Verify that the date format is correct

        #region Verify date format        //Check whether the date format is correct        public static bool IsDate(string str)
        {
            // Considering the 4-year 366 days, there is also a special February date            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 (str);
        }
        #endregion

18. Verify whether the characters are between 4 and 12

        #region Verify whether the characters are between 4 and 12        bool IsValidByte(string strIn)
        {
            return (strIn, @"^[a-z]{4,12}$");
        }
        #endregion

19. Determine whether a string is a number

        #endregion
        #region determines whether a string is a number        /// &lt;summary&gt;
        /// Determine whether the string is a number        /// &lt;/summary&gt;
        /// <param name="str">character to be verified</param>        /// &lt;returns&gt;bool&lt;/returns&gt;
        public static bool IsNumber(string str)
        {
            bool result = true;
            foreach (char ar in str)
            {
                if (!(ar))
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
        #endregion

20. Is it digital?

        Is #region a digital type?        /// &lt;summary&gt;
        /// Is it digital?        /// &lt;/summary&gt;
        /// &lt;param name="strNumber"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsDecimal(string strNumber)
        {
            return new (@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
        }
        #endregion

21. Verify whether Chinese/all Chinese is included

        #region Verify whether Chinese/all Chinese is included        /// &lt;summary&gt;
        /// Verify whether Chinese is included        /// &lt;/summary&gt;
        /// &lt;param name="str"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsHanyu(string str)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]");
            if ((str).Success)
                return true;
            else
                return false;
        }
        /// &lt;summary&gt;
        /// Verify that all Chinese        /// &lt;/summary&gt;
        /// &lt;param name="str"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public static bool IsHanyuAll(string str)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]");
            // When the matching content length is the same as the verified content length, the verification passes            if ((str).Success)
                if ((str).Count == )
                    return true;
            //Other, not passed            return false;
        }
        #endregion

Summarize

This is the end of this article about the complete collection of C# regular expression verification methods. For more related C# regular expression verification content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!