add 新增 正则工具类 字符串提取 字符串校验

master
疯狂的狮子Li 2 years ago
parent 05880981f8
commit 2417517aee

@ -0,0 +1,49 @@
package org.dromara.common.core.constant;
import cn.hutool.core.lang.RegexPool;
/**
*
* <p>
* : https://any86.github.io/any-rule/
*
* @author Feng
*/
public interface RegexConstants extends RegexPool {
/**
* 线
*/
public static final String DICTIONARY_TYPE = "^[a-z][a-z0-9_]*$";
/**
* 6
*/
public static final String ID_CARD_LAST_6 = "^(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
/**
* QQ
*/
public static final String QQ_NUMBER = "^[1-9][0-9]\\d{4,9}$";
/**
*
*/
public static final String POSTAL_CODE = "^[1-9]\\d{5}$";
/**
*
*/
public static final String ACCOUNT = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
/**
* 8
*/
public static final String PASSWORD = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$";
/**
* 01
*/
public static final String STATUS = "^[01]$";
}

@ -0,0 +1,52 @@
package org.dromara.common.core.factory;
import cn.hutool.core.lang.PatternPool;
import org.dromara.common.core.constant.RegexConstants;
import java.util.regex.Pattern;
/**
*
* <p></p>
* <p></p>
*
* @author 21001
*/
public class RegexPatternPoolFactory extends PatternPool {
/**
* 线
*/
public static final Pattern DICTIONARY_TYPE = get(RegexConstants.DICTIONARY_TYPE);
/**
* 6
*/
public static final Pattern ID_CARD_LAST_6 = get(RegexConstants.ID_CARD_LAST_6);
/**
* QQ
*/
public static final Pattern QQ_NUMBER = get(RegexConstants.QQ_NUMBER);
/**
*
*/
public static final Pattern POSTAL_CODE = get(RegexConstants.POSTAL_CODE);
/**
*
*/
public static final Pattern ACCOUNT = get(RegexConstants.ACCOUNT);
/**
* 8
*/
public static final Pattern PASSWORD = get(RegexConstants.PASSWORD);
/**
* 01
*/
public static final Pattern STATUS = get(RegexConstants.STATUS);
}

@ -0,0 +1,30 @@
package org.dromara.common.core.utils.regex;
import cn.hutool.core.util.ReUtil;
import org.dromara.common.core.constant.RegexConstants;
/**
*
*
* @author Feng
*/
public final class RegexUtils extends ReUtil {
/**
*
*
* @param input
* @param regex 使 {@link RegexConstants}
* @param defaultInput
* @return
*/
public static String extractFromString(String input, String regex, String defaultInput) {
try {
return ReUtil.get(regex, input, 1);
} catch (Exception e) {
return defaultInput;
}
}
}

@ -0,0 +1,105 @@
package org.dromara.common.core.utils.regex;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.Validator;
import org.dromara.common.core.factory.RegexPatternPoolFactory;
import java.util.regex.Pattern;
/**
*
*
*
* @author Feng
*/
public class RegexValidator extends Validator {
/**
* 线
*/
public static final Pattern DICTIONARY_TYPE = RegexPatternPoolFactory.DICTIONARY_TYPE;
/**
* 6
*/
public static final Pattern ID_CARD_LAST_6 = RegexPatternPoolFactory.ID_CARD_LAST_6;
/**
* QQ
*/
public static final Pattern QQ_NUMBER = RegexPatternPoolFactory.QQ_NUMBER;
/**
*
*/
public static final Pattern POSTAL_CODE = RegexPatternPoolFactory.POSTAL_CODE;
/**
*
*/
public static final Pattern ACCOUNT = RegexPatternPoolFactory.ACCOUNT;
/**
* 8
*/
public static final Pattern PASSWORD = RegexPatternPoolFactory.PASSWORD;
/**
* 01
*/
public static final Pattern STATUS = RegexPatternPoolFactory.STATUS;
/**
*
*
* @param value
* @return true false
*/
public static boolean isAccount(CharSequence value) {
return isMatchRegex(ACCOUNT, value);
}
/**
* ValidateException
*
* @param value
* @param errorMsg
* @param <T> CharSequence
* @return
* @throws ValidateException
*/
public static <T extends CharSequence> T validateAccount(T value, String errorMsg) throws ValidateException {
if (!isAccount(value)) {
throw new ValidateException(errorMsg);
}
return value;
}
/**
*
*
* @param value
* @return true false
*/
public static boolean isStatus(CharSequence value) {
return isMatchRegex(STATUS, value);
}
/**
* ValidateException
*
* @param value
* @param errorMsg
* @param <T> CharSequence
* @return
* @throws ValidateException
*/
public static <T extends CharSequence> T validateStatus(T value, String errorMsg) throws ValidateException {
if (!isStatus(value)) {
throw new ValidateException(errorMsg);
}
return value;
}
}

@ -6,6 +6,7 @@ import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.dromara.common.core.constant.RegexConstants;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.system.domain.SysDictType;
@ -37,7 +38,7 @@ public class SysDictTypeBo extends BaseEntity {
*/
@NotBlank(message = "字典类型不能为空")
@Size(min = 0, max = 100, message = "字典类型类型长度不能超过{max}个字符")
@Pattern(regexp = "^[a-z][a-z0-9_]*$", message = "字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)")
@Pattern(regexp = RegexConstants.DICTIONARY_TYPE, message = "字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)")
private String dictType;
/**

Loading…
Cancel
Save