Regular expressions are often used to validate various forms, Java forms register common Regular Expression validation tool classes, and common regular expression large collections.
1. Telephone number
2. zip Code
3. QQ
4. E-Mail
5. Mobile phone number
6. URL
7. Whether it is a digital
8. Whether it is Chinese
9. Identity card
10. Domain Name
IP .....
Common validation Everything! This is indeed your work in Web development, server-side form verification of good products! You deserve to have ^_^
* * 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> * @create 2012-11-15 PM 4:54:42/Public CLA
SS Regexutils {/** * Verify email * @param email email address, format: zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx representative mail Service provider * @return Validation successfully returns TRUE, validation failure returns false */public static Boolean Checkemail (String email) {String regex = "\\w+@\\w+\ \. [A-z]+ (\\.[
a-z]+)? ";
Return pattern.matches (regex, email); /** * Authentication ID number * @param idcard resident ID number 15 or 18 digits, the last one may be a number or letter * @return validation successfully returns true, validation fails return false */Publ
IC 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 Internet Card) *, 150, 151, 152, 157 (TD-specific), 158, 159, 187 (not enabled), 188 (TD-specific) </p> *<p> Unicom's number: 130, 131, 132, 155, 156 (World Wind Dedicated), 185 (not enabled), 186 (3g) </p> *<p> Telecom Number: 133, 153, 180 (not enabled), 189</p> * Return validation successfully returns true, validation fails return false/public static Boolean Checkmobile (String mobile) {string regex = "(\\+\\d+)? 1
[34578]\\d{9}$];
Return pattern.matches (Regex,mobile); /** * Verify the fixed phone number * @param phone phone number, format: Country (region) telephone code + area code (city code) + telephone 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 (city codes):</b> this may contain one or more digits from 0 to 9, the region or city code is placed in parentheses--* the country (region) that does not use the region or city code, the component is omitted.
</p> * <p><b> Phone number:</b> This contains one or more digits from 0 to 9 </p> * @return Validation successfully 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); /** * validating integers (positive and negative integers) * @param digit Integer * between one or more than 0-9 * @return validation successfully returns true, validation fails return false */public static Boolean checkdigit (String digit) {string regex = ' \\-?[
1-9]\\d+ ";
Return pattern.matches (Regex,digit); /** * verifies integers and floating-point numbers (positive and negative integers) * @param decimals a floating-point number between one or more digits, such as: 1.23,233.30 * @return Validation returns true successfully, validation fails back FAL SE */public static Boolean Checkdecimals (String decimals) {string regex = ' \\-?[
1-9]\\d+ (\\.\\d+)? ";
Return pattern.matches (regex,decimals); /** * Verify that white space characters * @param blankspace white space characters, including: spaces, \ \ n, \ r, \f, \x0b * @return validation successfully returns TRUE, validation failure returns false */P
Ublic Static Boolean Checkblankspace (String blankspace) {string regex = ' \\s+ ';
Return pattern.matches (Regex,blankspace); /** * Verify that Chinese * @param Chinese characters * @return validation returns true successfully, validation failure returns false/public static Boolean Checkch
Inese (string Chinese) {string regex = ' ^[\u4e00-\u9fa5]+$ '; Return pattern.matches (Regex,chinese);
/** * Validation date (month/year) * @param birthday date, format: 1992-09-03, or 1992.09.03 * @return validation returns true successfully, validation fails return 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 validation successfully returns true, validation fails to return FALSE */public static Boolean checkurl (String URL) {St Ring regex = "(https?:/ /(w{3}\\.)? \\w+\\.\\w+ (\\.[ a-za-z]+) * (: \\d{1,5})? (/\\w*) * (\??) (.+=.*)?
(&.+=.*)? ";
Return pattern.matches (regex, url); /** * <pre> * Access URL of the first level domain * </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 domainName//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 postal code * @param postcode postal code * @return Validation returns true successfully, validation fails return false */public static Boolean Checkpo
Stcode (String postcode) {string regex = ' [1-9]\\d{5} ';
Return Pattern.matches (regex, postcode); /** * Match 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 successful return Tru E, validation failed to return 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);
}
}
Share a tool class that uses regular expressions to validate phone numbers, ID numbers, date formats, URLs, emails, and so on
Package com.eabax.util;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern; /** * Validation Tool class * @author admin * * */public class Validation {//------------------constant Definition/** * Email Regular expression = "^" ([A-Z 0-9a-z]+[-|\\.] +[a-z0-9a-z]@ ([a-z0-9a-z]+ (-[a-z0-9a-z]+)/\.)
+[a-za-z]{2,}$ "; * *//public static final String EMAIL = "^ ([a-z0-9a-z]+[-|\\.]?) +[a-z0-9a-z]@ ([a-z0-9a-z]+ (-[a-z0-9a-z]+)/\.)
+[a-za-z]{2,}$ ";;
public static final String EMAIL = "\\w+ (\\.\\w+) *@\\w+ (\\.\\w+) +"; /** * Phone number Regular expression = (^ (\d{2,4}[-_--]?)? \d{3,8} ([-_--]?\d{3,8})? ([-_--]?\d{1,7})? $) | (^0?1[35]\d{9}$) * * public static final String PHONE = "(^ (\\d{2,4}[-_--]?) \\d{3,8} ([-_--]?\\d{3,8})? ([-_--]?\\d{1,7})? $) |
(^0?1[35]\\d{9}$) "; /** * Mobile number Regular expression =^ (13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9]) \d{8}$ * * public static final String mobile = "^ (13[0-9)
|14[0-9]|15[0-9]|17[0-9]|18[0-9]) \\d{8}$ "; /** * An integer regular expression ^-? ([[1-9]\d*$) |] * * public static final String INTEGER = "^-?
([[1-9]\\d*$] |) ";
/** * Positive integer Regular expression >=0 ^[1-9]\d*|0$/public static final String integer_negative = "^[1-9]\\d*|0$";
/** * Negative integer regular expression <=0 ^-[1-9]\d*|0$/public static final String integer_positive = "^-[1-9]\\d*|0$"; /** * Double regular expression ^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $/public static final String DOUBLE = ' ^-?
([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0) $ "; /** * DOUBLE Regular expression >=0 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$/public static final String double_negative
= "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$"; /** * Negative DOUBLE Regular expression <= 0 ^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$/public static final String Double_pos
itive = "^ (-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*)) |0?\\.0+|0$"; /** * Age Regular Expression ^ (?: [1-9][0-9]?| 1[01][0-9]|120 $ match 0-120 years old/public static final String age= "^ (?: [1-9][0-9]?|
1[01][0-9]|120) $ "; /** * ZIP code regular expression [0-9]\d{5} (?! \d) Domestic 6 zip/public static final StRing code= "[0-9]\\d{5} (?!
\\d) ";
/** * matches a string of digits, 26 English letters or underscores ^\w+$/public static final string str_eng_num_= "^\\w+$";
/** * Matches a string of numbers and 26 English letters ^[a-za-z0-9]+$/public static final string str_eng_num= "^[a-za-z0-9]+";
/** * matches a string of 26 English letters ^[a-za-z]+$/public static final string str_eng= "^[a-za-z]+$"; /** * Filter Special string Regular * regex= "[' ~!@#$%^&* () +=|{} ':; ', \\[\\].<>/?~! @#¥%......&* ()--+| {}【】‘;:”“'。 ,、?
]"; * * public static final String str_special= "[' ~!@#$%^&* () +=|{} ':; ', \\[\\].<>/?~! @#¥%......&* ()--+| {}【】‘;:”“'。 ,、?
]"; /*** * date is supported: * YYYY-MM-DD * YYYY/MM/DD * YYYY_MM_DD * YYYYMMDD * * YYYY. Mm. DD Form/public static final String date_all= "(^ (1[8-9]\\d{2}) | ( [2-9]\\d{3}]) ([-\\/\\._]?) (10|12|0?) [13578]) ([-\\/\\._]?) (3[01]| [12] [0-9]|0? [1-9]) $)" +
"| (^ (1[8-9]\\d{2}) | ([2-9]\\d{3})] ([-\\/\\._]?) (11|0?) [469]) ([-\\/\\._]?) (30| [12] [0-9]|0? [1-9]) $)" +
"| (^ (1[8-9]\\d{2}) | ([2-9]\\D{3})) ([-\\/\\._]?) (0?2) ([-\\/\\._]?) (2[0-8]|1[0-9]|0?) [1-9]) $)| (^ ([2468][048]00) ([-\\/\\._]?) (0?2) ([-\\/\\._]?) (29) $) | (^ ([3579][26]00) "+" ([-\\/\\._]?) (0?2) ([-\\/\\._]?) (29) $) "+" | (^ ([1][89][0][48]) ([-\\/\\._]?) (0?2) ([-\\/\\._]?) (29) $) | (^ ([2-9][0-9][0][48]) ([-\\/\\._]?) "+" (0?2) ([-\\/\\._]?) (29) $) "+" | (^ ([1][89][2468][048]) ([-\\/\\._]?) (0?2) ([-\\/\\._]?) (29) $) | (^ ([2-9][0-9][2468][048]) ([-\\/\\._]?) (0?2) "+" ([-\\/\\._]?) (29) $) | (^ ([1][89][13579][26]) ([-\\/\\._]?) (0?2) ([-\\/\\._]?) (29) $) | " + "(^ ([2-9][0-9][13579][26]) ([-\\/\\._]?) (0?2) ([-\\/\\._]?)
(29) $)) "; /*** * Date Support: * YYYY-MM-DD/public static final String date_format1= "([0-9]{3}[1-9]|[ 0-9]{2}[1-9][0-9]{1}| [0-9] {1} [1-9] [0-9] {2}| [1-9] [0-9] {3}) -((0[13578]|1[02])-(0[1-9]|[ 12][0-9]|3[01]) | ((0[469]|11)-(0[1-9]|[ 12][0-9]|30)) | (02-(0[1-9]|[ 1][0-9]|2[0-8])) | (([0-9]{2}) (0[48]|[ 2468][048]| [13579] [26]) | ((0[48]| [2468] [048]| [3579] [26])
00)-02-29) "; /** * URL Regular tableUp-Match HTTP www ftp/public static final String URL = "^ (http|www|ftp|)?" (://)? (\\w+ (-\\w+) *) (\\. (\\w+ (-\\w+) *)) * ((: \\d+)?) ((\\w+ (-\\w+) *)) * (\.? (\\w) *) (\) "+" ((\\w*%) * (\\w*\\?) * (\\w*:) * (\\w*\\+) * (\\w*\\.) * (\\w*&) * (\\w*-) * (\\w*=) * (\\w*%) * (\\w*\\?) * "+" (\\w*:) * (\\w*\\+) * (\\w*\\.)
* "+" (\\w*&) * (\\w*-) * (\\w*=) *) * (\\w*) *) $ "; /** * Identity card Regular expression */public static final String idcard= (11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|
45|46|50|51|52|53|54|61|62|63|64|65) [0-9]{4}] "+" ([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3} + "[xx0-9]) |
([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3})] ";
/** * Organization Code/public static final String Jigou_code = "^[a-z0-9]{8}-[a-z0-9]$";
/** * Matching number of strings ^[0-9]+$ * * public static final String str_num = "^[0-9]+$";
------------------Verification Method/** * Determines whether the field is null and returns ture * @param str * @return Boolean/public static synchronized Boolean strisnull (String str) {return null = str | | str.trim (). Length () <= 0?
True:false; /** * Judge Field is NON-NULL compliant return ture * @param str * @return Boolean/public static Boolean strnotnull (String St R) {return!
Strisnull (str);
/** * string NULL nulls * @param str * @return Boolean/public static string Nulltostr (String str) {
return Strisnull (str)? ": str; /** * String NULL assignment default * @param str target String * @param defaut Default * @return String/public static
String Nulltostr (String str,string defaut) {return strisnull (str)? DEFAUT:STR; /** * Determines whether the field is email compliant return ture * @param str * @return Boolean/public static Boolean Isemail (String
STR) {return Regular (str,email); /** * Determines whether the phone number conforms to return ture * @param str * @return Boolean/public static Boolean Isphone (String str ) {return Regular (str,PHONE); /** * Determines whether the cell phone number conforms to the return ture * @param str * @return Boolean/public static Boolean IsMobile (String St
R) {return Regular (str,mobile);
/** * Determines whether the URL conforms to the return ture * @param str * @return Boolean/public static Boolean Isurl (String str) {
Return Regular (Str,url); /** * Determines whether the field is a numeric positive or negative integer positive or negative number conforming to return ture * @param str * @return Boolean/public static Boolean Isnum
ber (String str) {return Regular (str,double); /** * Determines whether the field is an integer conforming return ture * @param str * @return Boolean/public static boolean Isinteger (str
ing str) {return Regular (Str,integer); /** * Determines whether the field is a positive integer regular expression >=0 compliant return ture * @param str * @return Boolean/public static Boolean Isint
Eger_negative (String str) {return Regular (str,integer_negative); /** * Determines whether the field is a negative integer regular expression <=0 compliant return ture * @param str * @return Boolean/public static Boolean Isint Eger_pOsitive (String str) {return Regular (str,integer_positive); /** * Determines whether the field is double compliant returns TURE * @param str * @return Boolean/public static Boolean isdouble (str
ing str) {return Regular (str,double); /** * Determines whether the field is a positive floating-point number regular expression >=0 compliant return ture * @param str * @return Boolean/public static Boolean ISDO
Uble_negative (String str) {return Regular (str,double_negative); /** * Determines whether the field is a negative floating-point number regular expression <=0 compliant return ture * @param str * @return Boolean/public static Boolean ISDO
Uble_positive (String str) {return Regular (str,double_positive); /** * Determines whether the field is a date-compliant return ture * @param str * @return Boolean/public static Boolean isDate (String St
R) {return Regular (Str,date_all); /** * Verify 2010-12-10 * @param str * @return/public static Boolean isDate1 (String str) {return Re
Gular (STR,DATE_FORMAT1); /** * Determines whether the field is age compliant return ture * @param str *Return Boolean/public static Boolean isage (String str) {return Regular (str,age); /** * Determines whether the field is very long * string is empty return Fasle, exceeding length {leng} return ture vice return false * @param str * @param leng * @return Boo Lean */public static Boolean islengout (String Str,int Leng) {return strisnull (str)? False:str.trim (). length
() > Leng; /** * Determines whether the field is an identity card compliance return ture * @param str * @return Boolean/public static Boolean Isidcard (String s
TR) {if (Strisnull (str)) return false;
if (Str.trim (). Length () = = | Str.trim (). Length () = () {return Regular (Str,idcard);
}else {return false; }/** * Determines whether the field is postcode compliant return ture * @param str * @return Boolean/public static Boolean Iscode (String str)
{return Regular (Str,code); /** * Judge string is not all English letter * @param str * @return Boolean/public static Boolean Isenglish (String str) {return Regular (Str,str_eng) ; /** * Judge string is not all English letter + number * @param str * @return Boolean/public static Boolean Iseng_num (string s
TR) {return Regular (str,str_eng_num); /** * Judge string is not all English letter + digit + underline * @param str * @return Boolean/public static Boolean iseng_num_ (str
ing str) {return Regular (str,str_eng_num_); /** * Filter Special string returns filtered String * @param str * @return Boolean/public static string Filterstr (String str
{Pattern P = pattern.compile (str_special);
Matcher m = p.matcher (str);
Return M.replaceall (""). Trim (); /** * Calibration Mechanism code format * @return/public static Boolean Isjigoucode (String str) {return Regular (Str,jigou
_code); /** * Determines whether a string is a digital component * @param str * @return Boolean/public static Boolean Isstr_num (String str)
{return Regular (str,str_num); /** * Match matches regular expression pattern match returns true * @param str matched string * @param pattern matching mode *@return Boolean/private static Boolean Regular (String str,string pattern) {if null = str | | Str.trim (). Leng
Th () <=0) return false;
Pattern P = pattern.compile (pattern);
Matcher m = p.matcher (str);
return m.matches ();
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.