Common Java Regular Expression verification tool class RegexUtils. java, common java Regular Expressions

Source: Internet
Author: User

Common Java Regular Expression verification tool class RegexUtils. java, common java Regular Expressions

Original article: RegexUtils. Java, a common java Regular Expression verification tool

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

Java form registration common Regular Expression verification tool class, common Regular Expressions large set.

1. Phone number

2. Zip code

3. QQ

4. Email

5. Mobile phone number

6. URL

7. Is it a number?

8. Is it Chinese?

9. ID card

10. Domain Name

11. IP ....

Common verification is all you need! This is indeed a good product for web development and form verification on the server side! You are worth it

/** Copyright 2012-2013 The Haohui Network Corporation */package com. haohui. common. utils; import java. util. regex. matcher; import java. util. regex. pattern; /*** @ project baidamei * @ author cevencheng <cevencheng@gmail.com> www.zuidaima.com * @ create 2012-11-15 4:54:42 */public class RegexUtils {/*** verify Email * @ param email address, format: zhangsan@zuidaima.com, zhangsan@xxx.com.cn, xxx represents the mail service provider * @ return verification Returns true if the verification fails. 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, China Unicom, and China telecom operator number segment * <p> mobile number segment: 134 (0-8), 135, 136, 13 7. 138, 139, 147 (intended for TD Nic) *, 150, 151, 152, 157 (dedicated for TD), 158, 159, 187 (not enabled), 188 (dedicated to TD) </p> * <p> China Unicom CIDR blocks: 130, 131, 132, 155 (dedicated to world wind), 156 (not enabled), 186 (3g) </p> * <p> telecommunications segment: 133, 153, 180 (not enabled), 189 </p> * @ return returns true if verification succeeds, 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) telephone code + district code (city code) + phone number, such as: + 8602085588447 * <p> <B> country (region) code: </B> standard country (region) that identifies the country (region) of the phone number) code. It contains one or multiple digits from 0 to 9. * The digit is followed by a country (region) code 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> This includes one or more numbers from 0 to 9 </p> * @ return returns true if the verification is successful, if verification fails, false */public static boolean checkPhone (String phone) {String regex = "(\\++ \\ d +) is returned )? (\ 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, if verification fails, false */public static boolean checkChinese (String chinese) {String rege is returned. X = "^ [\ u4E00-\ 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/770596 0? 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 );} /*** <pre> * get the first-level domain name of the URL * 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); // obtain the complete 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 ();}/*** match the Chinese postal code * @ param postcode zip code * @ return returns true if the verification succeeds, 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) * @ pa Ram ipAddress IPv4 standard address * @ return returns true if verification succeeds, 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 );}}

 


Who has some common java Regular Expression tool classes, the more the better?

You can download expresso. I think it is very useful ~ Especially group display. It is very convenient for java to use matcher. group ().
 
Use a java regular expression to search for the source code of a string

Class used jakarta-oro-2.0.jar package, please download it on the apache website
This Java Regular Expression tool class currently mainly has 25 Regular Expressions, some of which are not commonly used. Here only 15 common Java regular expressions are listed:
1. matching images;
2. Match the email address;
3. Match and extract the url;
4. Match and extract http;
5. Matching date
6. Match the phone number;
7. Matching ID card
8 matching zip code
9. match without special characters
10 match a non-negative integer (positive integer + 0)
11 matching non-negative integers excluding zero (positive integers> 0)
12 match a positive integer
13 match non-positive integers (negative integers + 0)
14. match a negative integer;
15. Matching Integers

Package com. ygj. util; import java. util. *; import org. apache. oro. text. regex. *;/*** class Introduction: use regular expressions to verify or extract data. The methods in the class are all static. * main methods: 1. isHardRegexpValidate (String source, String regexp) specifies a regular expression that is case sensitive. * 2. isSoftRegexpValidate (String source, String regexp) * regular expressions that are case-insensitive * 3. getHardRegexpMatchResult (String source, String regexp) * returns the batch configuration result set (case-sensitive regular expression batch configuration) * 4. getSoftRegexpMatchResult (String source, String regexp) * returns the batch configuration result set (a formal case-insensitive expression batch configuration) * 5 getHardRegexpArray (String source, String regexp) * return the expected batch configuration result set (case-sensitive regular expression batch configuration) * 6. getSoftRegexpMatchResult (String source, String regexp) * returns the batch configuration result set (not large ...... remaining full text>
 

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.