SoFunction
Updated on 2025-03-01

Detailed explanation of the application of regular expressions in C#

C# regular validation collection () regular expression verification

Need to introduce a namespaceusing ;  

#region Verify that the text box is entered as a number    /// <summary>
    /// Verify whether it is a number (including integers and decimals)    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool GetNum(string str)
    {
      return (str, @"^[-]?\d+[.]?\d*$");
    }
    #endregion
    #region Verify that the text box is input as an integer    /// <summary>
    /// Verify that the text box is input as an integer    /// </summary>
    /// <param name="strNum">Input characters</param>    /// <returns>Return a value of type bool</returns>    public static bool validateNum(string strNum)
    {
      return (strNum, "^[0-9]*$");
    }
    #endregion
    #region Verify text box input as date    /// &lt;summary&gt;
    ///Judge date    /// &lt;/summary&gt;
    /// &lt;param name="Date"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool IsValidDate(string Date)
    {
      //Verify the YYYY-MM-DD format, basically taking into account leap years and February, etc.      bool bValid = (Date, @"^((((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-))$");
      return (bValid &amp;&amp; ("1753-01-01") &gt;= 0);
      //Merge the date verification expressions of plain and leap years, and we get the final regular expression with the format YYYY-MM-DD as:      //(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|
      //[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-
      //(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|
      //(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|
      //[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)
    }
    #endregion
    #region Verify text box input as email    //Verify email    public static bool IsValidEmail(string strIn)
    {
      return (strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
    }
    #endregion
    #region Verify text box to enter as a phone number    /// &lt;summary&gt;
    /// Verify the text box to enter as a phone number    /// &lt;/summary&gt;
    /// <param name="strPhone">Input string</param>    /// <returns>Return a value of type bool</returns>    public static bool validatePhone(string strPhone)
    {
      return (strPhone, @"\d{3,4}-\d{7,8}");
    }
    #endregion
    #region Verify text box input as a fax number    /// &lt;summary&gt;
    /// Verify the text box to enter as a fax number    /// &lt;/summary&gt;
    /// <param name="strFax">Input string</param>    /// <returns>Return a value of type bool</returns>    public static bool validateFax(string strFax)
    {
      return (strFax, @"86-\d{2,3}-\d{7,8}");
    }
    #endregion
    #region Verify whether it is an ip    //Get the IP string    /// &lt;summary&gt;
    /// Is it an ip    /// &lt;/summary&gt;
    /// &lt;param name="ip"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool IsIP(string ip)
    {
      return (, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
    }
    public static bool IsIPSect(string ip)
    {
      return (, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$");
    }
    #endregion
    #region Verify whether the string is a yy-mm-dd string    /// &lt;summary&gt;
    /// Determine whether the string is a yy-mm-dd string    /// &lt;/summary&gt;
    /// &lt;param name="str"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static bool IsDateString(string str)
    {
      return (str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
    }
    #endregion

The above is the application of regular expressions in C# introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!