mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-10 14:19:08 +00:00
add 正则工具类,新增字符串提取,字符串校验
This commit is contained in:
@@ -0,0 +1,85 @@
|
|||||||
|
package org.dromara.common.core.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正则表达式
|
||||||
|
*
|
||||||
|
* @author Feng
|
||||||
|
*/
|
||||||
|
public interface RegexConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中文字符正则表达式
|
||||||
|
*/
|
||||||
|
public static final String CHINESE_REGEX = "[\\u4e00-\\u9fa5]+";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 姓名(2-4个中文字符正则)
|
||||||
|
*/
|
||||||
|
public static final String NAME_REGEX = "^[\u4e00-\u9fa5]{2,4}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 匹配中国大陆手机号码
|
||||||
|
*/
|
||||||
|
public static final String PHONE_NUMBER_REGEX = "^(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 LANDLINE_REGEX = "^(0\\d{2,3})-?(\\d{7,8})$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 电子邮箱
|
||||||
|
*/
|
||||||
|
public static final String EMAIL_REGEX = "^[\\w+&*-]+(?:\\.[\\w+&*-]+)*@(?:[\\w+&*-]+\\.)+[a-zA-Z]{2,7}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证号码(普通校验)
|
||||||
|
*/
|
||||||
|
public static final String ID_CARD_REGEX_GENERAL = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证号码(精准校验 - 18位)
|
||||||
|
*/
|
||||||
|
public static final String ID_CARD_REGEX_ACCURATE_18 = "^[1-9]\\d{5}(19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证号码(15位)
|
||||||
|
*/
|
||||||
|
public static final String ID_CARD_REGEX_ACCURATE_15 = "^[1-9]\\d{5}\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{2}[0-9Xx]$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证号码(后6位)
|
||||||
|
*/
|
||||||
|
public static final String ID_CARD_REGEX_LAST_6 = "^(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* QQ号码
|
||||||
|
*/
|
||||||
|
public static final String QQ_NUMBER_REGEX = "^[1-9][0-9]\\d{4,9}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮政编码
|
||||||
|
*/
|
||||||
|
public static final String POSTAL_CODE_REGEX = "^[1-9]\\d{5}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册账号
|
||||||
|
*/
|
||||||
|
public static final String ACCOUNT_REGEX = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码:包含至少8个字符,包括大写字母、小写字母、数字和特殊字符
|
||||||
|
*/
|
||||||
|
public static final String PASSWORD_REGEX = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用状态(0表示正常,1表示停用)
|
||||||
|
*/
|
||||||
|
public static final String STATUS_REGEX = "^[01]$";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)
|
||||||
|
*/
|
||||||
|
public static final String DICTIONARY_TYPE_REGEX = "^[a-z][a-z0-9_]*$";
|
||||||
|
|
||||||
|
}
|
@@ -2,6 +2,8 @@ package org.dromara.common.core.utils;
|
|||||||
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import org.dromara.common.core.constant.RegexConstants;
|
||||||
|
import org.dromara.common.core.exception.ServiceException;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -10,15 +12,92 @@ import java.util.Set;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正则工具类
|
||||||
|
*
|
||||||
|
* @author Feng
|
||||||
|
*/
|
||||||
public class ReUtil {
|
public class ReUtil {
|
||||||
public final static Pattern GROUP_VAR = Pattern.compile("\\$(\\d+)");
|
public final static Pattern GROUP_VAR = Pattern.compile("\\$(\\d+)");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 正则中需要被转义的关键字
|
* 正则中需要被转义的关键字
|
||||||
*/
|
*/
|
||||||
public final static Set<Character> RE_KEYS = new HashSet<>(
|
public final static Set<Character> RE_KEYS = new HashSet<>(Arrays.asList('$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|'));
|
||||||
Arrays.asList('$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|'));
|
|
||||||
;
|
/**
|
||||||
|
* 判断字符串是否匹配指定的正则表达式
|
||||||
|
*
|
||||||
|
* @param input 要检查的字符串
|
||||||
|
* @param regex 用于匹配的正则表达式,可以使用 {@link RegexConstants} 中定义的常量
|
||||||
|
* @return 如果字符串与正则表达式匹配,返回 true;否则返回 false
|
||||||
|
* @throws IllegalArgumentException 如果输入字符串或正则表达式为 null
|
||||||
|
*/
|
||||||
|
public static boolean isValid(String input, String regex) {
|
||||||
|
// 检查输入参数是否为null,如果是则抛出IllegalArgumentException
|
||||||
|
if (StringUtils.isEmpty(input) || StringUtils.isEmpty(regex)) {
|
||||||
|
throw new ServiceException("输入和正则表达式不得为空");
|
||||||
|
}
|
||||||
|
// 编译正则表达式
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
// 创建匹配器对象,并将输入字符串与正则表达式进行匹配
|
||||||
|
Matcher matcher = pattern.matcher(input);
|
||||||
|
// 返回匹配结果
|
||||||
|
return matcher.matches();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从输入字符串中提取匹配的部分
|
||||||
|
*
|
||||||
|
* @param input 要提取的输入字符串
|
||||||
|
* @param regex 用于匹配的正则表达式,可以使用 {@link RegexConstants} 中定义的常量
|
||||||
|
* @return 如果找到匹配的部分,则返回匹配的部分,否则返回原始输入字符串
|
||||||
|
*/
|
||||||
|
public static String extractFromString(String input, String regex) {
|
||||||
|
// 检查输入参数是否为null,如果是则抛出IllegalArgumentException
|
||||||
|
if (StringUtils.isEmpty(input) || StringUtils.isEmpty(regex)) {
|
||||||
|
throw new ServiceException("输入和正则表达式不得为空");
|
||||||
|
}
|
||||||
|
// 编译正则表达式
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
// 创建匹配器
|
||||||
|
Matcher matcher = pattern.matcher(input);
|
||||||
|
// 查找匹配
|
||||||
|
if (matcher.find()) {
|
||||||
|
// 获取匹配的部分
|
||||||
|
return matcher.group(1);
|
||||||
|
} else {
|
||||||
|
// 如果没有匹配,返回原始字符串
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从输入字符串中提取匹配的部分,如果没有匹配则返回默认值
|
||||||
|
*
|
||||||
|
* @param input 要提取的输入字符串
|
||||||
|
* @param regex 用于匹配的正则表达式,可以使用 {@link RegexConstants} 中定义的常量
|
||||||
|
* @param defaultInput 如果没有匹配时返回的默认值
|
||||||
|
* @return 如果找到匹配的部分,则返回匹配的部分,否则返回默认值
|
||||||
|
*/
|
||||||
|
public static String extractFromString(String input, String regex, String defaultInput) {
|
||||||
|
// 检查输入参数是否为null,如果是则抛出IllegalArgumentException
|
||||||
|
if (StringUtils.isEmpty(input) || StringUtils.isEmpty(regex)) {
|
||||||
|
throw new ServiceException("输入和正则表达式不得为空");
|
||||||
|
}
|
||||||
|
// 编译正则表达式
|
||||||
|
Pattern pattern = Pattern.compile(regex);
|
||||||
|
// 创建匹配器
|
||||||
|
Matcher matcher = pattern.matcher(input);
|
||||||
|
// 查找匹配
|
||||||
|
if (matcher.find()) {
|
||||||
|
// 获取匹配的部分
|
||||||
|
return matcher.group(1);
|
||||||
|
} else {
|
||||||
|
// 如果没有匹配,返回默认值
|
||||||
|
return defaultInput;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 正则替换指定值<br>
|
* 正则替换指定值<br>
|
||||||
|
@@ -1,17 +1,17 @@
|
|||||||
package org.dromara.system.domain.bo;
|
package org.dromara.system.domain.bo;
|
||||||
|
|
||||||
import io.github.linpeilie.annotations.AutoMapper;
|
import io.github.linpeilie.annotations.AutoMapper;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.EqualsAndHashCode;
|
|
||||||
import org.dromara.common.core.validate.AddGroup;
|
|
||||||
import org.dromara.common.core.validate.EditGroup;
|
|
||||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
|
||||||
import org.dromara.system.domain.SysDictType;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Pattern;
|
import jakarta.validation.constraints.Pattern;
|
||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.dromara.common.core.constant.RegexConstants;
|
||||||
|
import org.dromara.common.core.validate.AddGroup;
|
||||||
|
import org.dromara.common.core.validate.EditGroup;
|
||||||
|
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||||
|
import org.dromara.system.domain.SysDictType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典类型业务对象 sys_dict_type
|
* 字典类型业务对象 sys_dict_type
|
||||||
@@ -42,7 +42,7 @@ public class SysDictTypeBo extends BaseEntity {
|
|||||||
*/
|
*/
|
||||||
@NotBlank(message = "字典类型不能为空", groups = { AddGroup.class, EditGroup.class })
|
@NotBlank(message = "字典类型不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||||
@Size(min = 0, max = 100, message = "字典类型类型长度不能超过{max}个字符")
|
@Size(min = 0, max = 100, message = "字典类型类型长度不能超过{max}个字符")
|
||||||
@Pattern(regexp = "^[a-z][a-z0-9_]*$", message = "字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)")
|
@Pattern(regexp = RegexConstants.DICTIONARY_TYPE_REGEX, message = "字典类型必须以字母开头,且只能为(小写字母,数字,下滑线)")
|
||||||
private String dictType;
|
private String dictType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user