343 lines
8.7 KiB
Java
343 lines
8.7 KiB
Java
package ink.wgink.util;
|
|
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
/**
|
|
* When you feel like quitting. Think about why you started
|
|
* 当你想要放弃的时候,想想当初你为何开始
|
|
*
|
|
* @ClassName: RegexUtil
|
|
* @Description: 正则校验工具类
|
|
* @Author: WangGeng
|
|
* @Date: 2019/12/4 18:13
|
|
* @Version: 1.0
|
|
**/
|
|
public class RegexUtil {
|
|
|
|
/**
|
|
* 特殊字符
|
|
*/
|
|
public static final String SPECIAL_CHARACTERS = "#?!@$%^&*-_";
|
|
/**
|
|
* 密码最小长度
|
|
*/
|
|
public static final int PASSWORD_LENGTH_MIN = 6;
|
|
/**
|
|
* 密码最大长度
|
|
*/
|
|
public static final int PASSWORD_LENGTH_MAX = 16;
|
|
/**
|
|
* 手机
|
|
*/
|
|
private static final Pattern PATTERN_PHONE = Pattern.compile("^1\\d{10}$");
|
|
/**
|
|
* 邮箱
|
|
*/
|
|
private static final Pattern PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
|
|
/**
|
|
* URL
|
|
*/
|
|
private static final Pattern PATTERN_URL = Pattern.compile("(^#)|(^http(s*):\\/\\/[^\\s]+)");
|
|
/**
|
|
* 路径
|
|
*/
|
|
private static final Pattern PATTERN_PATH = Pattern.compile("\\/[^\\s]*");
|
|
/**
|
|
* 日期格式
|
|
*/
|
|
private static final Pattern PATTERN_DATE = Pattern.compile("^(\\d{4})[-\\/](\\d{1}|0\\d{1}|1[0-2])([-\\/](\\d{1}|0\\d{1}|[1-2][0-9]|3[0-1]))*$");
|
|
/**
|
|
* 时间戳格式
|
|
*/
|
|
private static final Pattern PATTERN_DATETIME = Pattern.compile("^(\\d{4})[-\\/](\\d{1}|0\\d{1}|1[0-2])([-\\/](\\d{1}|0\\d{1}|[1-2][0-9]|3[0-1]))*(\\s+)([0-1][0-9]|(2[0-3])):([0-5][0-9])(:([0-5][0-9]))*$");
|
|
/**
|
|
* 时间戳
|
|
*/
|
|
private static final Pattern PATTERN_YYYY_MM_DD = Pattern.compile("^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[1-2][0-9]|3[0-1])$");
|
|
/**
|
|
* 时间戳
|
|
*/
|
|
private static final Pattern PATTERN_YYYY_MM_DD_HH_MM_SS = Pattern.compile("^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[1-2][0-9]|3[0-1])([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])$");
|
|
/**
|
|
* 时间戳(毫秒)
|
|
*/
|
|
private static final Pattern PATTERN_YYYY_MM_DD_HH_MM_SS_ZZZ = Pattern.compile("^(\\d{4})(0\\d{1}|1[0-2])(0\\d{1}|[1-2][0-9]|3[0-1])([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])(\\d{3})$");
|
|
/**
|
|
* 时间戳
|
|
*/
|
|
private static final Pattern PATTERN_HH_MM_SS = Pattern.compile("^([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])$");
|
|
/**
|
|
* 时间戳(毫秒)
|
|
*/
|
|
private static final Pattern PATTERN_HH_MM_SS_ZZZ = Pattern.compile("^([0-1][0-9]|(2[0-3]))([0-5][0-9])([0-5][0-9])(\\d{3})$");
|
|
/**
|
|
* 身份证
|
|
*/
|
|
private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)");
|
|
/**
|
|
* 用户名
|
|
*/
|
|
private static final Pattern PATTERN_USERNAME = Pattern.compile("^[a-zA-Z0-9\\-\\_\\!\\@\\#\\%\\.]+$");
|
|
/**
|
|
* 字母
|
|
*/
|
|
private static final Pattern PATTERN_LETTER = Pattern.compile("[a-zA-Z]+");
|
|
/**
|
|
* 字母和数字
|
|
*/
|
|
private static final Pattern PATTERN_LETTER_OR_NUMBER = Pattern.compile("[a-zA-Z0-9]+");
|
|
/**
|
|
* 中文
|
|
*/
|
|
private static final Pattern PATTERN_CHINESE = Pattern.compile("[\\u4e00-\\u9fa5]+");
|
|
/**
|
|
* 数字
|
|
*/
|
|
private static final Pattern PATTERN_NUMBER = Pattern.compile("\\d+");
|
|
/**
|
|
* 弱密码
|
|
*/
|
|
private static final Pattern PASSWORD_WEEK = Pattern.compile(String.format("(?=.*[A-Za-z0-9%s]).{%d,%d}", SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));
|
|
/**
|
|
* 中密码
|
|
*/
|
|
private static final Pattern PASSWORD_MIDDLE = Pattern.compile(String.format("((?=.*[A-Za-z])(?=.*[0-9]))|((?=.*[A-Za-z])(?=.*[%s]))|((?=.*[0-9])(?=.*[%s])).{%d,%d}", SPECIAL_CHARACTERS, SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));
|
|
/**
|
|
* 强密码
|
|
*/
|
|
private static final Pattern PASSWORD_STRONG = Pattern.compile(String.format("(?=.*[A-Za-z])(?=.*[0-9])(?=.*[%s]).{%d,%d}", SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX));
|
|
/**
|
|
* 路径参数
|
|
*/
|
|
private static final Pattern PATH_PARAMS = Pattern.compile("\\{\\w+\\}");
|
|
|
|
/**
|
|
* 判断弱密码强度
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isPasswordWeek(String input) {
|
|
return PASSWORD_WEEK.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断中密码强度
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isPasswordMiddle(String input) {
|
|
return PASSWORD_MIDDLE.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断强密码强度
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isPasswordStrong(String input) {
|
|
return PASSWORD_STRONG.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断电话
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isPhone(String input) {
|
|
return PATTERN_PHONE.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断邮箱
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isEmail(String input) {
|
|
return PATTERN_EMAIL.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断URL
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isUrl(String input) {
|
|
return PATTERN_URL.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断PATH
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isPath(String input) {
|
|
return PATTERN_PATH.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断日期
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isDate(String input) {
|
|
return PATTERN_DATE.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断时间戳
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isDatetime(String input) {
|
|
return PATTERN_DATETIME.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断时间时间戳
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isYyyyMmDd(String input) {
|
|
return PATTERN_YYYY_MM_DD.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断时间时间戳
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isYyyyMmDdHhMmSs(String input) {
|
|
return PATTERN_YYYY_MM_DD_HH_MM_SS.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断时间时间戳
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isYyyyMmDdHhMmSsZzz(String input) {
|
|
return PATTERN_YYYY_MM_DD_HH_MM_SS_ZZZ.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断时间时间戳
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isHhMmSs(String input) {
|
|
return PATTERN_HH_MM_SS.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断时间时间戳
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isHhMmSsZzz(String input) {
|
|
return PATTERN_HH_MM_SS_ZZZ.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断身份证
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isIdentity(String input) {
|
|
return PATTERN_IDENTITY.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断用户名
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isUsername(String input) {
|
|
return PATTERN_USERNAME.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断是字母
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isLetter(String input) {
|
|
return PATTERN_LETTER.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断是字母或数字
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isLetterOrNumber(String input) {
|
|
return PATTERN_LETTER_OR_NUMBER.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断是中文
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isChinese(String input) {
|
|
return PATTERN_CHINESE.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 判断是数字
|
|
*
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isNumber(String input) {
|
|
return PATTERN_NUMBER.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 自定义判断
|
|
*
|
|
* @param customPattern
|
|
* @param input
|
|
* @return
|
|
*/
|
|
public static boolean isMatch(Pattern customPattern, String input) {
|
|
return customPattern.matcher(input).matches();
|
|
}
|
|
|
|
/**
|
|
* 将路径中的参数替换为指定的字符,返回替换后的结果
|
|
*
|
|
* @param path 路径
|
|
* @param replaceStr 替换后的字符
|
|
* @return
|
|
*/
|
|
public static String replacePathParams(String path, String replaceStr) {
|
|
Matcher matcher = PATH_PARAMS.matcher(path);
|
|
while (matcher.find()) {
|
|
String group = matcher.group();
|
|
path = path.replace(group, replaceStr);
|
|
}
|
|
return path;
|
|
}
|
|
|
|
}
|