工具類代碼如下: 直接拷貝調(diào)用就可以
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 創(chuàng)建時間: 2019/3/4
* 描述:正則表達式
*/
public class RegexpUtils {
/**
* 車牌號碼Pattern
*/
public static final Pattern PLATE_NUMBER_PATTERN = Pattern
.compile("^[\u0391-\uFFE5]{1}[a-zA-Z0-9]{6}$");
/**
* 證件號碼Pattern
*/
public static final Pattern ID_CODE_PATTERN = Pattern
.compile("^[a-zA-Z0-9]+$");
/**
* 編碼Pattern
*/
public static final Pattern CODE_PATTERN = Pattern
.compile("^[a-zA-Z0-9]+$");
/**
* 固定電話編碼Pattern
*/
public static final Pattern PHONE_NUMBER_PATTERN = Pattern
.compile("0\\d{2,3}-[0-9]+");
/**
* 郵政編碼Pattern
*/
public static final Pattern POST_CODE_PATTERN = Pattern.compile("\\d{6}");
/**
* 面積Pattern
*/
public static final Pattern AREA_PATTERN = Pattern.compile("\\d*.?\\d*");
/**
* 手機號碼Pattern
*/
public static final Pattern MOBILE_NUMBER_PATTERN = Pattern
.compile("\\d{11}");
String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
public static final Pattern ID_CARD = Pattern.compile("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}(" +
"(0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$");
/**
* 銀行帳號Pattern
*/
public static final Pattern ACCOUNT_NUMBER_PATTERN = Pattern
.compile("\\d{16,21}");
/**
* 車牌號碼是否正確
*
* @param s
* @return
*/
public static boolean isPlateNumber(String s) {
Matcher m = PLATE_NUMBER_PATTERN.matcher(s);
return m.matches();
}
/**
* 證件號碼是否正確
*
* @param s
* @return
*/
public static boolean isIDCode(String s) {
Matcher m = ID_CODE_PATTERN.matcher(s);
return m.matches();
}
/**
* 編碼是否正確
*
* @param s
* @return
*/
public static boolean isCode(String s) {
Matcher m = CODE_PATTERN.matcher(s);
return m.matches();
}
/**
* 固話編碼是否正確
*
* @param s
* @return
*/
public static boolean isPhoneNumber(String s) {
Matcher m = PHONE_NUMBER_PATTERN.matcher(s);
return m.matches();
}
/**
* 郵政編碼是否正確
*
* @param s
* @return
*/
public static boolean isPostCode(String s) {
Matcher m = POST_CODE_PATTERN.matcher(s);
return m.matches();
}
/**
* 面積是否正確
*
* @param s
* @return
*/
public static boolean isArea(String s) {
Matcher m = AREA_PATTERN.matcher(s);
return m.matches();
}
/**
* 手機號碼否正確
*
* @param s
* @return
*/
public static boolean isMobileNumber(String s) {
Matcher m = MOBILE_NUMBER_PATTERN.matcher(s);
return m.matches();
}
public static boolean isIdCard(String s){
Matcher m = ID_CARD.matcher(s);
return m.matches();
}
/**
* 銀行賬號否正確
*
* @param s
* @return
*/
public static boolean isAccountNumber(String s) {
Matcher m = ACCOUNT_NUMBER_PATTERN.matcher(s);
return m.matches();
}
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
}