Java Regular Expressions (2). Common Regular Expressions

Source: Internet
Author: User

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:
[Java]
/**
* Regular Expression Tool
* Provides methods for verifying email addresses, mobile phone numbers, phone numbers, ID card numbers, and numbers.
*/
Public final class RegexUtils {
 
/**
* Verify Email
* @ Param email address, format: zhangsan@sina.com, zhangsan@xxx.com.cn, xxx on behalf of the mail service provider
* @ Return returns true if verification is successful, and false if verification fails.
*/
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: ID card number: 15 or 18 digits. the last digit may be a number or letter.
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkIdCard (String idCard ){
String regex = "[1-9] \ d {13, 16} [a-zA-Z0-9] {1 }";
Return Pattern. matches (regex, idCard );
}

/**
* Verify the mobile phone number (in international format, + 86135xxxx... (Mainland China), + 00852137xxxx... (Hong Kong, China ))
* @ Param mobile: Number segments of mobile, Unicom, and telecom carriers
* <P> Number segment of the mobile device: 134 (0-8), 135, 136, 137, 138, 139 (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) </p>
* <P> China Unicom number segment: 130, 131, 132, 155, 156 (dedicated to world wind), 185 (not activated), 186 (3g) </p>
* <P> telecommunications segment: 133, 153, 180 (not activated), 189 </p>
* @ Return returns true if verification is successful, and false if verification fails.
*/
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) phone code + district code (city code) + phone number, for example: + 8602085588447
* <P> <B> country (region) code: </B> the country (region) code that identifies the telephone number. It contains one or multiple digits from 0 to 9,
* The numbers are followed by country (region) codes separated by spaces. </P>
* <P> <B> area code (city code): </B> This may contain one or more numbers, ranging from 0 to 9. The region or city code is enclosed in parentheses --
* This component is omitted for countries (regions) that do not use the region or city code. </P>
* <P> <B> phone number </B>: it contains one or more numbers from 0 to 9. </p>
* @ Return returns true if verification is successful, and false if verification fails.
*/
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 verification is successful, and false if verification fails.
*/
Public static boolean checkDigit (String digit ){
String regex = "\\-? [1-9] \ d + ";
Return Pattern. matches (regex, digit );
}

/**
* Verify the integer and floating-point numbers (positive and negative integers and positive and negative floating-point numbers)
* @ Param decimals floating point number between one or more digits 0-9, for example, 1.23, 233.30
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkDecimals (String decimals ){
String regex = "\\-? [1-9] \ d + (\. \ d + )? ";
Return Pattern. matches (regex, decimals );
}

/**
* Verify white space characters
* @ Param blankSpace blank characters, including: space, \ t, \ n, \ r, \ f, \ x0B
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkBlankSpace (String blankSpace ){
String regex = "\ s + ";
Return Pattern. matches (regex, blankSpace );
}

/**
* Verify Chinese Characters
* @ Param chinese Characters
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkChinese (String chinese ){
String regex = "^ [\ u4E00-\ u9FA5] + $ ";
Return Pattern. matches (regex, chinese );
}

/**
* Verification date (year, month, day)
* @ Param birthday date, in the format of 1992-09-03 or 1992.09.03
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkBirthday (String birthday ){
String regex = "[1-9] {4} ([-./]) \ d {1, 2} \ 1 \ d {1 }";
Return Pattern. matches (regex, birthday );
}

/**
* Verify the URL address
* @ Param url format: http://blog.csdn.net: 80/xyang81/article/details/7705960? Or http://www.csdn.net: 80
* @ Return returns true if verification is successful, and false if verification fails.
*/
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 );
}

/**
* Match the Chinese postal code
* @ Param postcode zip code
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkPostcode (String postcode ){
String regex = "[1-9] \ d {5 }";
Return Pattern. matches (regex, postcode );
}

/**
* IP address matching (simple match, format, for example: 192.168.1.1, 127.0.0.1, no matching IP address segment size)
* @ Param ipAddress: IPv4 standard address
* @ Return returns true if verification is successful, and false if verification fails.
*/
Public static boolean checkIpAddress (String ipAddress ){
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 );
}

}
Complete unit test code:
[Java]
/**
* Regular Expression Tool Test
*/
Public class RegexUtilsTest {

/**
* Verify email
*/
@ Test
Public void testCheckEmail (){
Boolean result = RegexUtils. checkEmail ("zha2_ngsan@sina.com.cn ");
Assert. assertTrue (result );
}

/**
* Verify the ID card number
*/
@ Test
Public void testCheckIdCard (){
Boolean result = RegexUtils. checkIdCard ("432403193902273273 ");
Assert. assertTrue (result );
}

/**
* Verify the mobile phone number
*/
@ Test
Public void testCheckMobile (){
Boolean result = RegexUtils. checkMobile ("+ 8613620285733 ");
Assert. assertTrue (result );
}

/**
* Verify the phone number
*/
@ Test
Public void testCheckPhone (){
Boolean result = RegexUtils. checkPhone ("+ 860738-4630706 ");
Assert. assertTrue (result );
}

/**
* Verify the integer (positive integer and negative integer)
*/
@ Test
Public void testCheckDigit (){
Boolean result = RegexUtils. checkDigit ("123132 ");
Assert. assertTrue (result );
}

/**
* Verify decimals and integers (positive and negative integers and positive and negative decimals)
*/
@ Test
Public void testCheckDecimals (){
Boolean result = RegexUtils. checkDecimals ("-33.2 ");
Assert. assertTrue (result );
}

/**
* Verify white space characters
*/
@ Test
Public void testCheckBlankSpace (){
Boolean result = RegexUtils. checkBlankSpace ("");
Assert. assertTrue (result );
}

/**
* Match Chinese Characters
*/
@ Test
Public void testCheckChinese (){
Boolean result = RegexUtils. checkChinese ("Chinese ");
Assert. assertTrue (result );
}

/**
* Verification date
*/
@ Test
Public void testCheckBirthday (){
Boolean result = RegexUtils. checkBirthday ("1992/09/03 ");
Assert. assertTrue (result );
}

/**
* Verify China postal code
*/
@ Test
Public void testCheckPostcode (){
Boolean result = RegexUtils. checkPostcode ("417100 ");
Assert. assertTrue (result );
}

/**
* Verify the URL address
*/
@ Test
Public void testCheckURL (){
Boolean result = RegexUtils. checkURL ("http://blog.csdn.com: 80/xyang81/article/details? Name = & abc = Chinese ");
Assert. assertTrue (result );
}

/**
* Verify the IP address
*/
@ Test
Public void testCheckIpAddress (){
Boolean result = RegexUtils. checkIpAddress ("192.1.22.255 ");
Assert. assertTrue (result );
}
}
Test results:

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.