Java Common Regular Expression validation tool class Regexutils.java

Source: Internet
Author: User

Original: Java common regular Expression validation tool class Regexutils.java

Source code: Http://www.zuidaima.com/share/1550463379442688.htm

Java Forms register a common regular expression validation tool class, a large collection of commonly used regular expressions.

1. Phone number

2. Postal code

3. QQ

4. E-Mail

5. Mobile phone number

6. URL

7. Is the number

8. Whether it is Chinese

9. Identity card

10. Domain Name

IP ....

Common verification everything! This is indeed your web development, server-side forms validation of the product! You, deserve to have ^_^

/* Copyright 2012-2013 the Haohui Network Corporation */package com.haohui.common.utils;import Java.util.regex.Matche R;import java.util.regex.pattern;/** * @project Baidamei * @author Cevencheng <[email protected]> www.zuidaima.com * @create 2012-11-15 pm 4:54:42 */public class Regexutils {/** * Verify email * @param e-mail address, grid Formula: [Email protected],[email protected],xxx on behalf of the Mail service provider * @return Verify Success returns TRUE, validation failure returns false */public static B Oolean checkemail (String email) {String 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 validation returns true successfully, 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 to be used for TD card) *, 150, 151, 152, 157 (TD dedicated ), 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> *& Lt;p> Telecom Number segment: 133, 153, 180 (not enabled), 189</p> * @return Verify Success returns TRUE, validation failure returns false */public static Boolean Checkmo         Bile (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 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 code:</b> This may contain one or more numbers from 0 to 9, area or city code placed in parentheses--* for countries that do not use a region or city code, omit the component 。     </p> * <p><b> Phone number:</b> This contains one or more digits from 0 to 9 </p> * @return Verify Success returns TRUE, validation failure 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, validation 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 Validation successfully returns true, validation failed 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: space, \ t, \ n, \ r, \f, \x0b * @return Validation returns true successfully, validation failure returns false         */public static Boolean Checkblankspace (String blankspace) {string regex = "\\s+";     Return pattern.matches (Regex,blankspace); }/** * Verify Chinese * @param Chinese characters * @return validation returns true successfully, 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 * @return 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/7705960?         or http://www.csdn.net:80 * @return Verify Success 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); }/** * <pre> * Get the URL of the first-level domain name * http://www.zuidaima.com/share/1550463379442688.htm> zuidaima.com * </pre> * * @param URL * @return */public static string GetDomain (string URL) {Pattern p = pattern.compile ("(? <=http://|\\.) [^.] *?\\. (COM|CN|NET|ORG|BIZ|INFO|CC|TV) ", pattern.case_insensitive);//Get the full domain name//Pattern p=pattern.compile (" [^//]*?\\. ( COM|CN|NET|ORG|BIZ|INFO|CC|TV) ", pattern.case_insensitive);    Matcher Matcher = p.matcher (URL); Matcher.find (); return Matcher.group (); }/** * Matching China postcode * @param postcode ZIP code * @return Verify Success returns TRUE, validation failure returns false */public static Boolean C         Heckpostcode (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 Standard address * @return validation Successful 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);     }      }

Java Common Regular Expression validation tool class Regexutils.java

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.