常用正则匹配工具类
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;
@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)$";
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);
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);
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]");
public static boolean verifyRequestMethod(String requestMethod) {
return REQUEST_METHOD_PATTERN.matcher(requestMethod).matches();
}
public static boolean verifyPasswordStrict(String password) {
return PASSWORD_STRICT_PATTERN.matcher(password).matches();
}
public static boolean verifyNumberChar(String numberChar) {
return NUMBER_CHAR_PATTERN.matcher(numberChar).matches();
}
public static boolean verifyUsername(String username) {
return USERNAME_PATTERN.matcher(username).matches();
}
public static boolean verifyIp(String ip) {
return Boolean.TRUE.equals(isIpv6Address(ip)) ? Boolean.TRUE : IPV4_REG_PATTERN.matcher(ip).matches();
}
public static boolean verifyEmail(String email) {
return EMAIL_PATTERN.matcher(email).matches();
}
public static boolean verifyPhone(String phone) {
return TELEPHONE_PATTERN.matcher(phone).matches();
}
public static boolean verifyIdNum(String idNum) {
return ID_CARD_PATTERN.matcher(idNum).matches();
}
public static boolean verifyPassword(String password) {
return PASSWORD_PATTERN.matcher(password).matches();
}
public static boolean verifyLength(String str, Integer min, Integer max) {
return Pattern.matches("\\w*" + min + "," + max + "}", str);
}
public static boolean verifyNumber(String number) {
return NUMBER_PATTERN.matcher(number.trim()).matches();
}
public static boolean checkPass(String pass) {
return (pass.matches(".*[a-z]+.*") ||
pass.matches(".*[A-Z]+.*")) ||
pass.matches(".*\\d+.*") ||
pass.matches(".*[~!@#$%^&*.?]+.*");
}
public static boolean containChinese(String value) {
if (StringUtils.isBlank(value)) {
return Boolean.FALSE;
}
Matcher matcher = CHINESE.matcher(value);
return matcher.find();
}
public static Boolean isIpv6Address(String address) {
try {
final InetAddress inetAddress = InetAddress.getByName(address);
return inetAddress instanceof Inet6Address;
} catch (UnknownHostException e) {
return false;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)