From 2417517aeec7512b0580acd85eb7c5d26d2bdcc5 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期日, 14 一月 2024 20:44:45 +0800 Subject: [PATCH] add 新增 正则工具类 字符串提取 字符串校验 --- ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexValidator.java | 105 ++++++++++++++++++++++++++ ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/factory/RegexPatternPoolFactory.java | 52 +++++++++++++ ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysDictTypeBo.java | 3 ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/RegexConstants.java | 49 ++++++++++++ ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexUtils.java | 30 +++++++ 5 files changed, 238 insertions(+), 1 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/RegexConstants.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/RegexConstants.java new file mode 100644 index 0000000..b13c057 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/constant/RegexConstants.java @@ -0,0 +1,49 @@ +package org.dromara.common.core.constant; + +import cn.hutool.core.lang.RegexPool; + +/** + * 甯哥敤姝e垯琛ㄨ揪寮忓瓧绗︿覆 + * <p> + * 甯哥敤姝e垯琛ㄨ揪寮忛泦鍚堬紝鏇村姝e垯瑙�: 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,}$"; + + /** + * 閫氱敤鐘舵�侊紙0琛ㄧず姝e父锛�1琛ㄧず鍋滅敤锛� + */ + public static final String STATUS = "^[01]$"; + +} diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/factory/RegexPatternPoolFactory.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/factory/RegexPatternPoolFactory.java new file mode 100644 index 0000000..fd907d2 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/factory/RegexPatternPoolFactory.java @@ -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; + +/** + * 姝e垯琛ㄨ揪寮忔ā寮忔睜宸ュ巶 + * <p>鍒濆鍖栫殑鏃跺�欏皢姝e垯琛ㄨ揪寮忓姞鍏ョ紦瀛樻睜褰撲腑</p> + * <p>鎻愰珮姝e垯琛ㄨ揪寮忕殑鎬ц兘锛岄伩鍏嶉噸澶嶇紪璇戠浉鍚岀殑姝e垯琛ㄨ揪寮�</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); + + /** + * 閫氱敤鐘舵�侊紙0琛ㄧず姝e父锛�1琛ㄧず鍋滅敤锛� + */ + public static final Pattern STATUS = get(RegexConstants.STATUS); + +} diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexUtils.java new file mode 100644 index 0000000..b8b12d4 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexUtils.java @@ -0,0 +1,30 @@ +package org.dromara.common.core.utils.regex; + + +import cn.hutool.core.util.ReUtil; +import org.dromara.common.core.constant.RegexConstants; + +/** + * 姝e垯鐩稿叧宸ュ叿绫� + * + * @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; + } + } + +} diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexValidator.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexValidator.java new file mode 100644 index 0000000..c0dda20 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/regex/RegexValidator.java @@ -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; + +/** + * 姝e垯瀛楁鏍¢獙鍣� + * 涓昏楠岃瘉瀛楁闈炵┖銆佹槸鍚︿负婊¤冻鎸囧畾鏍煎紡绛� + * + * @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; + + /** + * 閫氱敤鐘舵�侊紙0琛ㄧず姝e父锛�1琛ㄧず鍋滅敤锛� + */ + 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; + } + +} diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysDictTypeBo.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysDictTypeBo.java index 39257f7..fcc1ac1 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysDictTypeBo.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/domain/bo/SysDictTypeBo.java @@ -6,6 +6,7 @@ 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 @@ */ @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; /** -- Gitblit v1.9.3