完成正则工具相关测试。

This commit is contained in:
2024-12-29 15:33:33 +08:00
parent 1727af5940
commit 979eedabb1
6 changed files with 486 additions and 66 deletions

View File

@@ -27,33 +27,70 @@ import java.util.regex.Pattern;
*/
public final class PatternConsts {
/** yyyyMMdd */
/**
* yyyyMMdd
*
* @see RegexConsts#BASIC_ISO_DATE
* </p>
*/
public static final Pattern BASIC_ISO_DATE = Pattern.compile(RegexConsts.BASIC_ISO_DATE);
/** yyyy-MM-dd */
/**
* yyyy-MM-dd
*
* @see RegexConsts#ISO_LOCAL_DATE
*/
public static final Pattern ISO_LOCAL_DATE = Pattern.compile(RegexConsts.ISO_LOCAL_DATE);
/** 密码 */
/**
* 密码
*
* @see RegexConsts#PASSWORD
*/
public static final Pattern PASSWORD = Pattern.compile(RegexConsts.PASSWORD);
/** 验证码 */
/**
* 验证码
*
* @see RegexConsts#CAPTCHA
*/
public static final Pattern CAPTCHA = Pattern.compile(RegexConsts.CAPTCHA);
/** 邮箱地址 */
/**
* 邮箱地址
*
* @see RegexConsts#EMAIL
*/
public static final Pattern EMAIL = Pattern.compile(RegexConsts.EMAIL);
/** 中国大陆手机号 */
/**
* 中国大陆手机号
*
* @see RegexConsts#MOBILE_PHONE
*/
public static final Pattern MOBILE_PHONE = Pattern.compile(RegexConsts.MOBILE_PHONE);
/** 用户名 */
/**
* 用户名
*
* @see RegexConsts#USERNAME
*/
public static final Pattern USERNAME = Pattern.compile(RegexConsts.USERNAME);
/** 昵称 */
/**
* 昵称
*
* @see RegexConsts#NICKNAME
*/
public static final Pattern NICKNAME = Pattern.compile(RegexConsts.NICKNAME);
/** 中国第二代居民身份证 */
/**
* 中国第二代居民身份证
*
* @see RegexConsts#CHINESE_2ND_ID_CARD_NUMBER
*/
public static final Pattern CHINESE_2ND_ID_CARD_NUMBER
= Pattern.compile(RegexConsts.CHINESE_2ND_ID_CARD_NUMBER);
= Pattern.compile(RegexConsts.CHINESE_2ND_ID_CARD_NUMBER, Pattern.CASE_INSENSITIVE);
private PatternConsts() {
throw new IllegalStateException("Utility class");

View File

@@ -24,9 +24,9 @@ package xyz.zhouxy.plusone.commons.constant;
*/
public final class RegexConsts {
public static final String BASIC_ISO_DATE = "^(?<y>\\d{4})(?<M>\\d{2})(?<d>\\d{2})";
public static final String BASIC_ISO_DATE = "^(?<yyyy>\\d{4,9})(?<MM>\\d{2})(?<dd>\\d{2})";
public static final String ISO_LOCAL_DATE = "^(?<y>\\d{4})-(?<M>\\d{2})-(?<d>\\d{2})";
public static final String ISO_LOCAL_DATE = "^(?<yyyy>\\d{4,9})-(?<MM>\\d{2})-(?<dd>\\d{2})";
public static final String PASSWORD = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[\\w\\\\!#$%&'*\\+\\-/=?^`{|}~@\\(\\)\\[\\]\",\\.;':><]{8,32}$";
@@ -38,12 +38,12 @@ public final class RegexConsts {
public static final String MOBILE_PHONE = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
public static final String USERNAME = "^[\\w_.@\\\\]{4,36}$";
public static final String USERNAME = "^[\\w-_.@]{4,36}$";
public static final String NICKNAME = "^[\\w_.@\\\\]{4,36}$";
public static final String NICKNAME = "^[\\w-_.@]{4,36}$";
public static final String CHINESE_2ND_ID_CARD_NUMBER
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dXx])$";
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dX])$";
private RegexConsts() {
throw new IllegalStateException("Utility class");

View File

@@ -89,22 +89,6 @@ public final class RegexTools {
return getPatternsInternal(patterns);
}
/**
* 手动缓存 Pattern 实例。
*
* @param pattern 要缓存的 {@link Pattern} 实例
* @return 缓存的 Pattern 实例。如果缓存已满,则返回 {@code null}。
*/
public static Pattern cachePattern(final Pattern pattern) {
AssertTools.checkNotNull(pattern, "The pattern can not be null.");
if (PATTERN_CACHE.size() >= MAX_CACHE_SIZE) {
return null;
}
final String patternStr = pattern.pattern();
final Pattern pre = PATTERN_CACHE.putIfAbsent(patternStr, pattern);
return pre != null ? pre : pattern;
}
/**
* 判断 {@code input} 是否匹配 {@code pattern}。
*
@@ -285,7 +269,6 @@ public final class RegexTools {
* 获取 {@link Pattern} 实例。
*
* @param pattern 正则表达式
* @param cachePattern 是否缓存 {@link Pattern} 实例
* @return {@link Pattern} 实例
*/
@Nonnull