Android often uses the regular tool class

Source: Internet
Author: User

This class provides regular validation functions that are often used in daily development. For example: Mailbox, phone number, phone number, identity card number, date, number, decimal, URL, IP address, etc. Using the matches method of the pattern object to match the entire character, calling the method is equivalent to:
Pattern p = pattern.compile (regex);
Matcher m = p.matcher (input);
return m.matches ();
Each of the regular may still need to be optimized, if you have a better way to achieve a certain function of validation, welcome to discuss together.

The following is the complete code for the tool class:

Source code address : http://download.csdn.net/detail/u014608640/7316565

/** * Regular Tool class * provides verification email, mobile phone number, phone number, ID number, number and other methods */public final class Regexutils {/** * Verify email * @param email address, format: [email Protected],[email Protected],xxx on behalf of the Mail service provider * @return Verify successful return True. Validation failure returns false */public static Boolean checkemail (String email) {String regex = "\\[email protected]\\w+\\.[ a-z]+ (\\.[ a-z]+)?

"; return pattern.matches (regex, email);} /** * Verify ID number * @param idcard resident ID number 15 or 18 digits. The last one may be a number or a letter * @return validation successfully returns TRUE, validation failure returns false */public static Boolean Checkidcard (String idcard) {string regex = "[1-9]\\d {13,16} [A-za-z0-9] {1} "; return Pattern.matches (Regex,idcard);} /** * Verify mobile phone number (support international format.) +86135xxxx ... (Mainland China). +00852137xxxx ... (Hong Kong, China) * @param Mobile Mobile, Unicom, telecom operator's number segment *<p> moving segment: 134 (0-8), 135, 136, 137, 138, 139, 147 (estimated for TD card) *, 150, 151, 152, 157 (TD-only), 158, 159, 187 (not enabled), 188 (TD-only) </p> *<p> unicom number segment: 130, 131, 132, 155, 156 (world-specific), 185 (not enabled), 186 (3g) </ P> *<p> Telecom Number segment: 133, 153, 180 (not enabled), 189</p> * @return Verify successful return True. Validation failure returns false */public static Boolean Checkmobile (String mobile) {string regex = "(\\+\\d+)? 1[3458]\\d{9}$"; return Pattern . Matches (regex,mobile);} /** * Verify fixed phone number * @param phone phone number, format: Country (region) phone code + area code (city codes) + phone number, such as: +8602085588447 * <p><b> Country (region) code: </b& The standard country (region) code for the country (region) that identifies the phone number. It includes one or more digits from 0 to 9, followed by a space-delimited code for the country (region). </p> * <p><b> area codes (city code):</b> this may include one or more numbers from 0 to 9. The region or city code is placed in parentheses--* the component is omitted for countries that do not use a region or city code. </p> * <p><b> Phone number:</b> This includes one or more digits from 0 to 9 </p> * @return Verify Success returns TRUE, validation failure returns false */public STA Tic Boolean Checkphone (String phone) {string regex = "(\\+\\d+)? ( \\d{3,4}\\-?)?

\\d{7,8}$ "; return pattern.matches (regex, phone);} /** * Verify integers (positive and negative integers) * @param digit integer between one or more bits 0-9 * @return Validation returns true successfully, validation failure returns false */public static Boolean checkdigit (Strin G digit) {String regex = "\\-"?

[1-9]\\d+]; return pattern.matches (Regex,digit);} /** * validates integers and floating-point numbers (plus and minus integers and positive and negative floating-point numbers) * @param decimals a floating-point number between one or more bits 0-9, such as: 1.23,233.30 * @return Validation returns true successfully, validation failure returns false */public Stati C Boolean checkdecimals (String decimals) {string regex = "\\-?[ 1-9]\\d+ (\\.\\d+)? "; Return pattern.matches (regex,decimals);} /** * Verify whitespace characters * @param blankspace whitespace characters, including: space, \ t, \ n, \ r, \f, \x0b * @return Validation returns true successfully, validation failure returns false */public static Boolean Chec Kblankspace (String blankspace) {string regex = "\\s+"; return pattern.matches (Regex,blankspace);} /** * Verify Chinese * @param Chinese Characters * @return Verify successful return True. Validation failure returns false */public static Boolean Checkchinese (string Chinese) {string regex = "^[\u4e00-\u9fa5]+$"; return Pattern.matches (Regex,chinese);} /** * Verification Date (Month day) * @param birthday date, format: 1992-09-03, or 1992.09.03 * @return Verify Success returns TRUE, validation failure returns false */public static Boolean ch Eckbirthday (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 validation successfully returns TRUE, validation failure returns false */public static Boolean checkurl (string url) {string regex = "(h ttps?:/ /(w{3}\\.)?

)? \\w+\\.\\w+ (\\.[ a-za-z]+) * (: \\d{1,5})? (/\\w*) * (\ \?

?

(.+=.*)?

(&.+=.*)?

)?"; Return pattern.matches (regex, url);} /** * Matching China ZIP code * @param postcode ZIP code * @return Verify Success returns TRUE, validation failure returns false */public static Boolean Checkpostcode (String Postco de) {String regex = "[1-9]\\d{5}"; return Pattern.matches (regex, postcode);} /** * matches IP address (simple match, format. such as: 192.168.1.1,127.0.0.1, no matching IP segment size) * @param ipAddress IPV4 Standard address * @return Verify Success returns TRUE, validation failure returns false */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);}}


 * */public class Regexutilstest {/** * Verify mailbox */@Testpublic void Testcheckemail () {Boolean result = REGEXUTILS.C Heckemail ("[email protected]"); Assert.asserttrue (result);} /** * Verify the identity card number */@Testpublic void Testcheckidcard () {Boolean result = Regexutils.checkidcard ("432403193902273273"); Assert.asserttrue (result);} /** * Verify mobile phone number */@Testpublic void Testcheckmobile () {Boolean result = Regexutils.checkmobile ("+8613620285733"); Assert.asserttrue (result);} /** * Verify Phone number */@Testpublic void Testcheckphone () {Boolean result = Regexutils.checkphone ("+860738-4630706"); Assert.asserttrue (result);} /** * Verify integers (positive and negative integers) */@Testpublic void Testcheckdigit () {Boolean result = Regexutils.checkdigit ("123132"); Assert.asserttrue (result);} /** * Verify decimals and integers (plus and minus integers and positive and negative numbers) */@Testpublic void Testcheckdecimals () {Boolean result = Regexutils.checkdecimals ("33.2"); Assert.asserttrue (result);} /** * Verify whitespace characters */@Testpublic void Testcheckblankspace () {Boolean result = Regexutils.checkblankspace (""); Assert.asserttrue (resULT);} /** * Match Chinese */@Testpublic void Testcheckchinese () {Boolean result = Regexutils.checkchinese ("Chinese"); Assert.asserttrue (result);} /** * Validation date */@Testpublic void Testcheckbirthday () {Boolean result = Regexutils.checkbirthday ("1992/09/03"); Assert.asserttrue (result);} /** * Verify China postcode */@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);}}


Android often uses the regular tool class

Related Article

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.