常用正则匹配工具类
PatternConstant.java
package com.base.common.constant;
import org.apache.commons.lang3.StringUtils;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author ming
*/
@SuppressWarnings("unused")
public abstract class PatternConstant {
private PatternConstant() {
}
/**
* 身份证正则
*/
public static final String ID_CARD_REG = "^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
public static final Pattern ID_CARD_PATTERN = Pattern.compile(ID_CARD_REG);
/**
* 手机正则
*/
public static final String TELEPHONE_REG = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
public static final Pattern TELEPHONE_PATTERN = Pattern.compile(TELEPHONE_REG);
/**
* 邮箱正则
*/
public static final String EMAIL_REG = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)$";
public static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REG);
public static final String IPV4_REG_START = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.";
public static final String IPV4_REG_MIDDLE = "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.";
public static final String IPV4_REG_END = "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
/**
* IPV4正则
*/
public static final String IPV4_REG = IPV4_REG_START + IPV4_REG_MIDDLE + IPV4_REG_MIDDLE + IPV4_REG_END;
public static final Pattern IPV4_REG_PATTERN = Pattern.compile(IPV4_REG);
/**
* 首个必须是字母,并且只由数字和字母组成,长度4-20位
*/
public static final String NUMBER_CHAR_REG = "^[a-zA-Z][a-zA-Z0-9]{3,19}$";
public static final Pattern NUMBER_CHAR_PATTERN = Pattern.compile(NUMBER_CHAR_REG);
public static final Pattern USERNAME_PATTERN = Pattern.compile(NUMBER_CHAR_REG);
/**
* 密码正则
*/
public static final String PASSWORD_REG = "^[a-zA-Z0-9_.]{6,20}$";
public static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REG);
/**
* 强密码规则,密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符
*/
public static final String PASSWORD_STRICT_REG = "^.*(?=.{6,})(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$";
public static final Pattern PASSWORD_STRICT_PATTERN = Pattern.compile(PASSWORD_STRICT_REG);
/**
* 请求方法正则
*/
public static final String REQUEST_METHOD_REG = "^(GET|HEAD|TRACE|OPTIONS)$";
public static final Pattern REQUEST_METHOD_PATTERN = Pattern.compile(REQUEST_METHOD_REG);
/**
* 数字
*/
public static final String NUMBER_REG = "^-?[0-9]+$";
public static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_REG);
/**
* 中文
*/
public static final Pattern CHINESE = Pattern.compile("[\u4e00-\u9fa5]");
/**
* 验证请求方式
*
* @param requestMethod 请求方式
* @return boolean
* @author ming
* @date 2022/6/7 22:54
*/
public static boolean verifyRequestMethod(String requestMethod) {
return REQUEST_METHOD_PATTERN.matcher(requestMethod).matches();
}
/**
* 验证密码(严格)
*
* @param password 密码
* @return boolean
* @author ming
* @date 2022/6/7 22:54
*/
public static boolean verifyPasswordStrict(String password) {
return PASSWORD_STRICT_PATTERN.matcher(password).matches();
}
/**
* 验证字母和字符串
*
* @param numberChar 字母和字符串
* @return boolean
* @author ming
* @date 2022/6/7 22:54
*/
public static boolean verifyNumberChar(String numberChar) {
return NUMBER_CHAR_PATTERN.matcher(numberChar).matches();
}
/**
* 验证用户名
*
* @param username 用户名
* @return boolean
* @author ming
* @date 2022/6/7 22:54
*/
public static boolean verifyUsername(String username) {
return USERNAME_PATTERN.matcher(username).matches();
}
/**
* 验证ip
*
* @param ip ip
* @return boolean
* @author ming
* @date 2022/6/7 22:54
*/
public static boolean verifyIp(String ip) {
return Boolean.TRUE.equals(isIpv6Address(ip)) ? Boolean.TRUE : IPV4_REG_PATTERN.matcher(ip).matches();
}
/**
* Verify mailbox format
*
* @param email 邮箱
* @return boolean
* @author ming
* @date 2022/6/7 22:43
*/
public static boolean verifyEmail(String email) {
return EMAIL_PATTERN.matcher(email).matches();
}
/**
* Verify cell-phone number format
*
* @param phone 手机号
* @return boolean
* @author ming
* @date 2022/6/7 22:43
*/
public static boolean verifyPhone(String phone) {
return TELEPHONE_PATTERN.matcher(phone).matches();
}
/**
* Verify ID number format
*
* @param idNum 身份证
* @return boolean
* @author ming
* @date 2022/6/7 22:44
*/
public static boolean verifyIdNum(String idNum) {
return ID_CARD_PATTERN.matcher(idNum).matches();
}
/**
* 验证密码
*
* @param password 密码
* @return boolean
* @author ming
* @date 2022/6/7 22:45
*/
public static boolean verifyPassword(String password) {
return PASSWORD_PATTERN.matcher(password).matches();
}
/**
* 验证字符串长度
*
* @param str 字符串
* @param min 最小
* @param max 最大
* @return boolean
* @author ming
* @date 2022/6/7 22:45
*/
public static boolean verifyLength(String str, Integer min, Integer max) {
return Pattern.matches("\\w*" + min + "," + max + "}", str);
}
/**
* Verify whether it is a pure number (can contain negative numbers, can't have spaces)
*
* @param number 数字
* @return boolean
* @author ming
* @date 2022/6/7 22:50
*/
public static boolean verifyNumber(String number) {
return NUMBER_PATTERN.matcher(number.trim()).matches();
}
/**
* 校验密码
*
* @param pass 密码
* @return boolean
* @author ming
* @date 2022/6/7 22:49
*/
public static boolean checkPass(String pass) {
return (pass.matches(".*[a-z]+.*") ||
pass.matches(".*[A-Z]+.*")) ||
pass.matches(".*\\d+.*") ||
pass.matches(".*[~!@#$%^&*.?]+.*");
}
/**
* 判断是否包含中文
*
* @param value 内容
* @return 结果
*/
public static boolean containChinese(String value) {
if (StringUtils.isBlank(value)) {
return Boolean.FALSE;
}
Matcher matcher = CHINESE.matcher(value);
return matcher.find();
}
/**
* 判断是否为ipv6地址
*
* @param address ip
* @return boolean
*/
public static Boolean isIpv6Address(String address) {
try {
final InetAddress inetAddress = InetAddress.getByName(address);
return inetAddress instanceof Inet6Address;
} catch (UnknownHostException e) {
return false;
}
}
}