This class provides common Regular Expression verification functions in daily development, such as email, mobile phone number, phone number, ID card number, date, number, decimal number, URL, IP address, and so on. Use the matches method of the Pattern object to match the entire character. Calling this method is equivalent:
Pattern p = Pattern. compile (regex );
Matcher m = p. matcher (input );
Return m. matches ();
Each regular expression may still need to be optimized. If you have a better way to verify a function, you are welcome to discuss it together. The complete code of the tool class is as follows:
Source Code address: http://download.csdn.net/detail/u014608640/7316565
/*** The regular expression tool provides methods for verifying Email addresses, mobile phone numbers, phone numbers, ID card numbers, and numbers */public final class RegexUtils {/*** verification email ** @ param email address, format: zhangsan@sina.com, zhangsan@xxx.com.cn, xxx represents the mail service provider * @ return verification success returns true, verification failure returns false */public static boolean checkEmail (String email) {String regex = "\ w + \\. [a-z] + (\\. [a-z] + )? "; Return Pattern. matches (regex, email);}/*** verify the ID card number * @ param idCard resident ID card number 15 or 18 digits, the last digit may be a number or letter * @ return. If the verification succeeds, true is returned. If the verification fails, false */public static boolean checkIdCard (String idCard) is returned) {String regex = "[1-9] \ d {13, 16} [a-zA-Z0-9] {1}"; return Pattern. matches (regex, idCard);}/*** verify the mobile phone number (supports international format, + 86135xxxx... (Mainland China), + 00852137xxxx... (Hong Kong, China) * @ param mobile, Unicom, and telecom operator number segment *Mobile Number segment: 134 (0-8), 135, 136, 137, 138, 139, 147 (expected to be used for the TD network card) *, 150, 151, 152, 157 (for TD purposes), 158, 159, 187 (not enabled), 188 (for TD purposes)
*China Unicom No.: 130, 131, 132, 155, 156 (dedicated to world wind), 185 (not activated), 186 (3g)
*Telecommunications segment: 133, 153, 180 (not activated), 189
* @ Return: returns true if verification succeeds. If verification fails, returns false */public static boolean checkMobile (String mobile) {String regex = "(\\++ \\ d + )? 1 [3458] \ d {9} $ "; return Pattern. matches (regex, mobile);}/*** verify the landline number * @ param phone number. Format: country (region) telephone code + district code (city code) + phone number, for example, + 8602085588447 *Country code:The Standard Country Code that identifies the country (region) of the phone number. It contains one or multiple digits from 0 to 9. * The digit is followed by a country (region) code separated by spaces.
*Area code (city code ):This may contain one or more numbers ranging from 0 to 9. The region or city code is placed in parentheses-* For countries (regions) that do not use the region or city code ), this component is omitted.
*Phone number:This contains one or more numbers from 0 to 9.
* @ Return: returns true if verification succeeds. If verification fails, returns false */public static boolean checkPhone (String phone) {String regex = "(\\++ \\ d + )? (\ D {3, 4 }\\-?)? \ D {7, 8} $ "; return Pattern. matches (regex, phone);}/*** verify the integer (positive integer and negative integer) * @ param digit an integer between one or more digits 0-9 * @ return returns true if the verification is successful, false */public static boolean checkDigit (String digit) {String regex = "\\-? [1-9] \ d + "; return Pattern. matches (regex, digit);}/*** verify the floating point numbers between integers and floating point numbers (positive and negative integers and positive and negative floating point numbers) * @ param decimals one-or multiple-digit 0-9 floating point numbers, such: 1.23, 233.30 * @ return returns true if the verification succeeds, and false if the verification fails */public static boolean checkDecimals (String decimals) {String regex = "\\-? [1-9] \ d + (\. \ d + )? "; Return Pattern. matches (regex, decimals);}/*** verify the white space character * @ param blankSpace blank characters, including: space, \ t, \ n, \ r, \ f, \ x0B * @ return returns true if the verification is successful, and false if the verification fails */public static boolean checkBlankSpace (String blankSpace) {String regex = "\ s +"; return Pattern. matches (regex, blankSpace);}/*** verify chinese ** @ param chinese character * @ return returns true if the verification is successful, false */public static boolean checkChinese (String chinese) {String regex = "^ [\ u 4E00-\ u9FA5] + $ "; return Pattern. matches (regex, chinese);}/*** verification date (year, month, day) * @ param birthday date. Format: 1992-09-03, or 1992.09.03 * @ return: true is returned for successful verification, false */public static boolean checkBirthday (String birthday) {String regex = "[1-9] {4 }([-. /]) \ d {1, 2} \ 1 \ d {1, 2} "; return Pattern. matches (regex, birthday);}/*** verify URL address * @ param url format: http://blog.csdn.net: 80/xyang81/article/details/7705960? Or http://www.csdn.net: 80 * @ return returns true for successful verification, false for failed verification */public static boolean checkURL (String url) {String regex = "(https?: // (W {3 }\\.)?)? \ W + \. \ w + (\. [a-zA-Z] +) * (: \ d {1, 5 })? (/\ W *)*(\\?? (. + = .*)? (&. + = .*)?)? "; Return Pattern. matches (regex, url);}/*** matches the Chinese postal code * @ param postcode zip code * @ return: true is returned for successful verification, false */public static boolean checkPostcode (String postcode) {String regex = "[1-9] \ d {5}"; return Pattern. matches (regex, postcode);}/*** Match IP address (simple match, format, for example: 192.168.1.1, 127.0.0.1, no matching IP segment size) * @ param ipAddress IPv4 standard address * @ return returns true if verification succeeds, and false if verification fails */public static boolean checkIpAddress (String ip Address) {String regex = "[1-9] (\ d {1, 2 })? \. (0 | ([1-9] (\ d {1, 2 })?)) \. (0 | ([1-9] (\ d {1, 2 })?)) \. (0 | ([1-9] (\ d {1, 2 })?)) "; Return Pattern. matches (regex, ipAddress );}}
* Regular Expression tool test */public class RegexUtilsTest {/*** verification email */@ Testpublic void testCheckEmail () {boolean result = RegexUtils. checkEmail ("zha2_ngsan@sina.com.cn"); Assert. assertTrue (result);}/*** verify the ID card number */@ Testpublic void testCheckIdCard () {boolean result = RegexUtils. checkidcards ("432403193902273273"); Assert. assertTrue (result);}/*** verify the mobile phone number */@ Testpublic void testCheckMobile () {boolean result = regexti Ls. checkMobile (& quot; + 8613620285733 & quot;); Assert. assertTrue (result);}/*** verify the phone number */@ Testpublic void testCheckPhone () {boolean result = RegexUtils. checkPhone ("+ 860738-4630706"); Assert. assertTrue (result);}/*** verify the integer (positive integer and negative integer) */@ Testpublic void testCheckDigit () {boolean result = RegexUtils. checkDigit ("123132"); Assert. assertTrue (result);}/*** verify decimals and integers (positive and negative integers) */@ Testpublic void testCheckDecimals () {boolean Result = RegexUtils. checkDecimals ("-33.2"); Assert. assertTrue (result);}/*** verify the blank character */@ Testpublic void testCheckBlankSpace () {boolean result = RegexUtils. checkBlankSpace (""); Assert. assertTrue (result);}/*** match Chinese */@ Testpublic void testCheckChinese () {boolean result = RegexUtils. checkChinese ("Chinese"); Assert. assertTrue (result);}/*** verification date */@ Testpublic void testCheckBirthday () {boolean result = RegexU Tils. checkBirthday ("1992/09/03"); Assert. assertTrue (result);}/*** verify China Post code */@ Testpublic void testCheckPostcode () {boolean result = RegexUtils. checkPostcode ("417100"); Assert. assertTrue (result);}/*** verify URL address */@ Testpublic void testCheckURL () {boolean result = RegexUtils. checkURL ("http://blog.csdn.com: 80/xyang81/article/details? Name = & abc = Chinese "); Assert. assertTrue (result);}/*** verify IP Address */@ Testpublic void testCheckIpAddress () {boolean result = RegexUtils. checkIpAddress ("192.1.22.255"); Assert. assertTrue (result );}}