Android Common Regular Tool class

Source: Internet
Author: User

This class provides regular validation functions commonly used in daily development, such as: mailboxes, cell phone numbers, phone numbers, ID numbers, dates, numbers, decimals, URLs, IP addresses, and so on. 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:

    /** * Regular Tool class * provides verification email, mobile phone number, phone number, ID number, number, etc. */public final class Regexutils {/**           * Verify Email * @param email address, format: [EMAIL&NBSP;PROTECTED],[EMAIL&NBSP;PROTECTED],XXX representative email service provider * @return Verify Success returns TRUE, validation failure returns false */public static Boolean checkemail (String email) {STR ing regex = "\\[email protected]\\w+\\." [A-z]+ (\\.[              a-z]+)? ";          Return pattern.matches (regex, email); /** * Verify the identity card number * @param Idcard resident ID number 15 or 18 digits, the last one may be a number or a letter * @return validated as Work 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 number segment *<p> Moving segment: 134 (0-8), 135, 136, 137, 138, 139, 147 (expected for TD online Card) *, 150, 151, 152, 157 (TD-only), 158, 159, 187 (not enabled), 188 (TD-only) </p> *<p&           gt; China Unicom number segment: 130, 131, 132, 155, 156 (world-specific), 185 (not enabled), 186 (3g) </p> *<p> Telecom: 133, 153, 180 (not enabled), 189</p>              * @return Verify Success returns TRUE, validation failure returns false */public static Boolean Checkmobile (String mobile) {              String regex = "(\\+\\d+)? 1[3458]\\d{9}$";          Return pattern.matches (Regex,mobile); /** * Verify the fixed phone number * @param phone phone number, format: Country (region) phone code + area code (city codes) + phone number, such as: +86020855884 * <p><b> Country Code:</b> the standard Country (region) code for the country (region) that identifies the phone number. It contains one or more digits from 0 to 9, and the number is followed by a space-delimited country (region) code. </p> * <p><b> area codes (city code):</b> this may contain one or more numbers from 0 to 9, area or city codes placed in parentheses--* to countries that do not use regional or city codes Home (region), the component is omitted. </p> * <p><b> Phone number:</b> This contains one or more digits from 0 to 9 </p> * @return Verify Success returns True, validation failed        Returns false   */public static Boolean Checkphone (String phone) {string regex = "(\\+\\d+)?" ( \\d{3,4}\\-?)?              \\d{7,8}$ ";          Return pattern.matches (regex, phone); }/** * verifies integers (positive and negative integers) * @param digit integer between one or more bits 0-9 * @return Validation returns true successfully, Certificate Failure returns false */public static Boolean checkdigit (string 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 one or more 0-9 floating-point numbers, such as: 1.23,233.30              * @return Verify Success returns TRUE, validation failure returns false */public static Boolean checkdecimals (String decimals) { String regex = "\\-?"              [1-9]\\d+ (\\.\\d+)? ";          Return pattern.matches (regex,decimals);  /** * Verify whitespace characters * @param blankspace whitespace characters, including: spaces, \ t, \ n, \ r, \f, \x0b * @return     Verify success returns TRUE, validation failure returns false      */public static Boolean Checkblankspace (String blankspace) {string regex = "\\s+";          Return pattern.matches (Regex,blankspace);           }/** * Verify Chinese * @param Chinese Characters * @return Verify success returns TRUE, validation failure returns false              */public static Boolean Checkchinese (string Chinese) {string regex = "^[\u4e00-\u9fa5]+$";          Return pattern.matches (Regex,chinese); }/** * Verified date (Month day) * @param birthday date, format: 1992-09-03, or 1992.09.03 * @retur N Verify success returns TRUE, validation failure returns 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/770 5960? or http://www.csdn.net:80 * @reTurn validation successfully returns TRUE, validation failure returns false */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);           }/** * Matching China postcode * @param postcode ZIP code * @return Verify Success returns TRUE, validation failure returns false              */public static Boolean Checkpostcode (String postcode) {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 Quasi-address * @return Validation successfully 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);   }                }
     * Regular Expression Tool class Test */public class Regexutilstest {/** * Verify mailbox */ @Test public void Testcheckemail () {Boolean result = Regexutils.checkemail ("[email protected              ]");          Assert.asserttrue (result);              /** * Verify the identity card number */@Test public void Testcheckidcard () {              Boolean result = Regexutils.checkidcard ("432403193902273273");          Assert.asserttrue (result);              /** * Verify Phone number */@Test public void Testcheckmobile () {              Boolean result = Regexutils.checkmobile ("+8613620285733");          Assert.asserttrue (result);              }/** * Verify phone number */@Test public void Testcheckphone () {              Boolean result = Regexutils.checkphone ("+860738-4630706");      Assert.asserttrue (result);    }/** * verifies integers (positive and negative integers) */@Test public void Testcheckdigit () {              Boolean result = Regexutils.checkdigit ("123132");          Assert.asserttrue (result); }/** * Verify decimals and integers (plus and minus integers and positive and negative numbers) */@Test public void Testcheckdecimals              () {Boolean result = Regexutils.checkdecimals ("33.2");          Assert.asserttrue (result);              }/** * Verify whitespace characters */@Test public void Testcheckblankspace () {              Boolean result = Regexutils.checkblankspace ("");          Assert.asserttrue (result);              }/** * matches Chinese */@Test public void Testcheckchinese () {              Boolean result = Regexutils.checkchinese ("Chinese");          Assert.asserttrue (result);         }/** * Verify date */ @Test public void Testcheckbirthday () {Boolean result = Regexutils.checkbirthday ("1992/09/03");          Assert.asserttrue (result);              }/** * Verify China Postcode */@Test public void Testcheckpostcode () {              Boolean result = Regexutils.checkpostcode ("417100");          Assert.asserttrue (result);              }/** * Verify 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 IP Address */@Test public void testcheckipaddress () {              Boolean result = Regexutils.checkipaddress ("192.1.22.255");          Assert.asserttrue (result);   }      }

Android common regular tool class

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.