Share a tool class that uses regular expressions to verify phone number, ID number, date format, URL, Email and other formats
The code copy is as follows:
package com.eabax.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Verification tool class
* @author admin
*
*/
public class Validation {
//-----------------------------------------------------------------------------------------------------------------------------
/**
* Email regular expression="^([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 = "^([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 phone number regular expression =^(13[0-9]|15[0-9]|18[0-9])/d{8}$
*/
public static final String MOBILE ="^(13[0-9]|15[0-9]|18[0-9])//d{8}$";
/**
* Integer regular expression ^-?(([1-9]/d*$)|0)
*/
public static final String INTEGER = "^-?(([1-9]//d*$)|0)";
/**
* 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_POSITIVE ="^(-([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)$";
/**
* Postal code regular expression [0-9]/d{5}(?!/d) Domestic 6-digit postal code
*/
public static final String CODE="[0-9]//d{5}(?!//d)";
/**
* Match a string composed of numbers, 26 English letters or underscores ^/w+$
*/
public static final String STR_ENG_NUM_="^//w+$";
/**
* Match a string composed of numbers and 26 English letters^[A-Za-z0-9]+$
*/
public static final String STR_ENG_NUM="^[A-Za-z0-9]+";
/**
* Match the string consisting of 26 English letters^[A-Za-z]+$
*/
public static final String STR_ENG="^[A-Za-z]+$";
/**
* Filter special string regularity
* regEx="[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%…&*() ―+|{}【】';: "'. , ? ]";
*/
public static final String STR_SPECIAL="[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%…& *()―+|{}【】';:""'. , ? ]";
/***
* Date regular support:
* YYYY-MM-DD
* YYYY/MM/DD
* YYYY_MM_DD
* YYYYMMDD
* Form of YYYY.MM.DD
*/
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 regular 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 expression
* 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*)*)$";
/**
* ID 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}))";
/**
* Institutional code
*/
public static final String JIGOU_CODE = "^[A-Z0-9]{8}-[A-Z0-9]$";
/**
* Match the string ^[0-9]+$
*/
public static final String STR_NUM = "^[0-9]+$";
////---------------------------------------------------------------------------------------------------------------------------
/**
* Determine whether the field is empty and matches the return ture
* @param str
* @return boolean
*/
public static synchronized boolean StrisNull(String str) {
return null == str || str.trim().length() <= 0 ? true : false ;
}
/**
* Return the ture if the field is non-empty
* @param str
* @return boolean
*/
public static boolean StrNotNull(String str) {
return !StrisNull(str);
}
/**
* String null to empty
* @param str
* @return boolean
*/
public static String nulltoStr(String str) {
return StrisNull(str)?"":str;
}
/**
* String null assignment default value
* @param str Target string
* @param defaut default value
* @return String
*/
public static String nulltoStr(String str,String defaut) {
return StrisNull(str)?defaut:str;
}
/**
* Determine whether the field is Email and returns the ture
* @param str
* @return boolean
*/
public static boolean isEmail(String str) {
return Regular(str,EMAIL);
}
/**
* Determine whether the phone number meets the return ture
* @param str
* @return boolean
*/
public static boolean isPhone(String str) {
return Regular(str,PHONE);
}
/**
* Determine whether the mobile phone number meets the status
* @param str
* @return boolean
*/
public static boolean isMobile(String str) {
return Regular(str,MOBILE);
}
/**
* Determine whether it is Url and returns the ture
* @param str
* @return boolean
*/
public static boolean isUrl(String str) {
return Regular(str,URL);
}
/**
* Determine whether the field is a number, positive and negative integer, positive and negative floating point number, return the ture
* @param str
* @return boolean
*/
public static boolean isNumber(String str) {
return Regular(str,DOUBLE);
}
/**
* Determine whether the field is INTEGER and returns the ture
* @param str
* @return boolean
*/
public static boolean isInteger(String str) {
return Regular(str,INTEGER);
}
/**
* Determine whether the field is a positive integer regular expression >=0 and matches the return ture
* @param str
* @return boolean
*/
public static boolean isINTEGER_NEGATIVE(String str) {
return Regular(str,INTEGER_NEGATIVE);
}
/**
* Determine whether the field is a negative integer regular expression <=0 and matches the return ture
* @param str
* @return boolean
*/
public static boolean isINTEGER_POSITIVE(String str) {
return Regular(str,INTEGER_POSITIVE);
}
/**
* Determine whether the field is DOUBLE and returns the ture
* @param str
* @return boolean
*/
public static boolean isDouble(String str) {
return Regular(str,DOUBLE);
}
/**
* Determine whether the field is a positive floating point number regular expression >=0 to match the return ture
* @param str
* @return boolean
*/
public static boolean isDOUBLE_NEGATIVE(String str) {
return Regular(str,DOUBLE_NEGATIVE);
}
/**
* Determine whether the field is a negative floating point number regular expression <=0 and matches the return ture
* @param str
* @return boolean
*/
public static boolean isDOUBLE_POSITIVE(String str) {
return Regular(str,DOUBLE_POSITIVE);
}
/**
* Determine whether the field is dated and returns the ture
* @param str
* @return boolean
*/
public static boolean isDate(String str) {
return Regular(str,DATE_ALL);
}
/**
* Verification 2010-12-10
* @param str
* @return
*/
public static boolean isDate1(String str) {
return Regular(str,DATE_FORMAT1);
}
/**
* Return to the ture if the field is age-compliant
* @param str
* @return boolean
*/
public static boolean isAge(String str) {
return Regular(str,AGE);
}
/**
* Determine whether the field is too long
* If the string is empty, it returns fasle, if it exceeds the length {leng}, it returns ture, and if it returns false
* @param str
* @param leng
* @return boolean
*/
public static boolean isLengOut(String str,int length) {
return StrisNull(str)?false:str.trim().length() > length ;
}
/**
* Determine whether the field is an ID card and returns the ture
* @param str
* @return boolean
*/
public static boolean isIdCard(String str) {
if(StrisNull(str)) return false ;
if(str.trim().length() == 15 || str.trim().length() == 18) {
return Regular(str,IDCARD);
}else {
return false ;
}
}
/**
* Determine whether the field has a zip code that matches the return ture
* @param str
* @return boolean
*/
public static boolean isCode(String str) {
return Regular(str,CODE);
}
/**
* Determine whether all strings are English letters
* @param str
* @return boolean
*/
public static boolean isEnglish(String str) {
return Regular(str,STR_ENG);
}
/**
* Determine whether all strings are English letters + numbers
* @param str
* @return boolean
*/
public static boolean isENG_NUM(String str) {
return Regular(str,STR_ENG_NUM);
}
/**
* Determine whether all strings are English letters + numbers + underscores
* @param str
* @return boolean
*/
public static boolean isENG_NUM_(String str) {
return Regular(str,STR_ENG_NUM_);
}
/**
* Filter special strings return filtered strings
* @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();
}
/**
* Verify the code format of the mechanism
* @return
*/
public static boolean isJigouCode(String str){
return Regular(str,JIGOU_CODE);
}
/**
* Determine whether the string is composed of numbers
* @param str
* @return boolean
*/
public static boolean isSTR_NUM(String str) {
return Regular(str,STR_NUM);
}
/**
* Whether the match conforms to the regular expression pattern. Return true
* @param str matching string
* @param pattern Matching pattern
* @return boolean
*/
private static boolean Regular(String str,String pattern){
if(null == str || str.trim().length()<=0)
return false;
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
return m.matches();
}
}
The above is all about this article, I hope you like it.