C # Regular Expression usage,

Source: Internet
Author: User

C # Regular Expression usage,

C # Regex. IsMatch () Regular Expression Verification

The namespace using System. Text. RegularExpressions needs to be introduced;

# Enter a number in the region verification text box /// <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 Regex. isMatch (str, @ "^ [-]? \ D + [.]? \ D * $ ");} # endregion # enter an integer in the region verification text box /// <summary> /// enter an integer in the verification text box /// </summary> /// <param name = "strNum"> input character </param> /// <returns> returns a value of the bool type </returns> public static bool validateNum (string strNum) {return Regex. isMatch (strNum, "^ [0-9] * $ ");} # endregion # enter the Date in the region verification text box /// <summary> /// determine the Date /// </summary> /// <param name = "Date"> </ param> // <returns> </returns> public static bool IsValidD Ate (string Date) {// verify the YYYY-MM-DD format, basically the leap year and February and so on are taken into account bool bValid = Regex. isMatch (Date, @ "^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 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 & Date. compareTo ("1753-01-01")> = 0); // combines the expression of the date verification for the year of the current year and the year of the new year, we get the final validation date format as the regular expression of the YYYY-MM-DD: // ([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)-0 2-29)} # endregion # enter the region verification text box as email // verification email public static bool IsValidEmail (string strIn) {return Regex. isMatch (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 # enter the phone number in the region verification text box /// <summary> /// enter the phone number in the verification text box /// </summary> /// <param name =" strPhone "> input string </param> // <returns> returns a Boolean value </returns> public static bool validatePhone (string strPhone) {return Regex. isMatch (strPhone, @ "\ d {3, 4}-\ d {7, 8 }");} # endregion # enter a fax number in the region verification text box /// <summary> /// enter a fax number in the verification text box /// </summary> /// <param name =" strFax "> input string </param> // <returns> returns Boolean values </returns> public static bool validateFax (string strFax) {return Regex. isMatch (strFax, @ "86-\ d {2, 3}-\ d {7, 8}") ;}# endregion # region verifies whether it is an ip address // The string HttpContext for obtaining the IP address. current. request. userHostAddress /// <summary> /// whether it is an ip address // </summary> /// <param name = "ip"> </param> // <returns> </returns> public static bool IsIP (string ip) {return Regex. isMatch (HttpContext. current. request. userHostAddress, @ "^ (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) $ ");} Public static bool IsIPSect (string ip) {return Regex. isMatch (HttpContext. current. request. userHostAddress, @ "^ (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {2} (2 [0-4] \ d | 25 [0-5] | [01]? \ D? | \ *) \.) (2 [0-4] \ d | 25 [0-5] | [01]? \ D? | \ *) $ ");} # Endregion # region verify whether the string is a yy-mm-dd string // <summary> // determine whether the string is a yy-mm-dd string // </summary> // /<param name = "str"> </param> // <returns> </returns> public static bool IsDateString (string str) {return Regex. isMatch (str, @ "(\ d {4})-(\ d {1, 2})-(\ d {1, 2})") ;}# endregion

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.