Browse Source

fix Validator

Looly 5 years ago
parent
commit
b8f8a3277f

+ 2 - 0
CHANGELOG.md

@@ -29,6 +29,8 @@
 * 【core   】     DateUtil增加dayOfYear方法(pr#895@Github)
 * 【core   】     DateUtil增加dayOfYear方法(pr#895@Github)
 * 【core   】     DateUtil增加dayOfYear方法(pr#895@Github)
 * 【core   】     DateUtil增加dayOfYear方法(pr#895@Github)
 * 【http   】     HttpUtil增加downloadBytes方法(pr#895@Github)
 * 【http   】     HttpUtil增加downloadBytes方法(pr#895@Github)
+* 【core   】     isMactchRegex失效标记,增加isMatchRegex(issue#I1IPJG@Gitee)
+* 【core   】     优化Validator.isChinese
 
 
 ### Bug修复
 ### Bug修复
 * 【core   】     修复SimpleCache死锁问题(issue#I1HOKB@Gitee)
 * 【core   】     修复SimpleCache死锁问题(issue#I1HOKB@Gitee)

+ 28 - 15
hutool-core/src/main/java/cn/hutool/core/lang/Validator.java

@@ -328,17 +328,6 @@ public class Validator {
 	}
 	}
 
 
 	/**
 	/**
-	 * 通过正则表达式验证
-	 *
-	 * @param regex 正则
-	 * @param value 值
-	 * @return 是否匹配正则
-	 */
-	public static boolean isMactchRegex(String regex, CharSequence value) {
-		return ReUtil.isMatch(regex, value);
-	}
-
-	/**
 	 * 通过正则表达式验证<br>
 	 * 通过正则表达式验证<br>
 	 * 不符合正则抛出{@link ValidateException} 异常
 	 * 不符合正则抛出{@link ValidateException} 异常
 	 *
 	 *
@@ -350,7 +339,7 @@ public class Validator {
 	 * @throws ValidateException 验证异常
 	 * @throws ValidateException 验证异常
 	 */
 	 */
 	public static <T extends CharSequence> T validateMatchRegex(String regex, T value, String errorMsg) throws ValidateException {
 	public static <T extends CharSequence> T validateMatchRegex(String regex, T value, String errorMsg) throws ValidateException {
-		if (false == isMactchRegex(regex, value)) {
+		if (false == isMatchRegex(regex, value)) {
 			throw new ValidateException(errorMsg);
 			throw new ValidateException(errorMsg);
 		}
 		}
 		return value;
 		return value;
@@ -372,6 +361,19 @@ public class Validator {
 	/**
 	/**
 	 * 通过正则表达式验证
 	 * 通过正则表达式验证
 	 *
 	 *
+	 * @param regex 正则
+	 * @param value 值
+	 * @return 是否匹配正则
+	 * @deprecated 拼写错误,请使用{@link #isMatchRegex(String, CharSequence)}
+	 */
+	@Deprecated
+	public static boolean isMactchRegex(String regex, CharSequence value) {
+		return ReUtil.isMatch(regex, value);
+	}
+
+	/**
+	 * 通过正则表达式验证
+	 *
 	 * @param pattern 正则模式
 	 * @param pattern 正则模式
 	 * @param value   值
 	 * @param value   值
 	 * @return 是否匹配正则
 	 * @return 是否匹配正则
@@ -381,6 +383,17 @@ public class Validator {
 	}
 	}
 
 
 	/**
 	/**
+	 * 通过正则表达式验证
+	 *
+	 * @param regex 正则
+	 * @param value 值
+	 * @return 是否匹配正则
+	 */
+	public static boolean isMatchRegex(String regex, CharSequence value) {
+		return ReUtil.isMatch(regex, value);
+	}
+
+	/**
 	 * 验证是否为英文字母 、数字和下划线
 	 * 验证是否为英文字母 、数字和下划线
 	 *
 	 *
 	 * @param value 值
 	 * @param value 值
@@ -422,7 +435,7 @@ public class Validator {
 		if (max <= 0) {
 		if (max <= 0) {
 			reg = "^\\w{" + min + ",}$";
 			reg = "^\\w{" + min + ",}$";
 		}
 		}
-		return isMactchRegex(reg, value);
+		return isMatchRegex(reg, value);
 	}
 	}
 
 
 	/**
 	/**
@@ -954,13 +967,13 @@ public class Validator {
 	}
 	}
 
 
 	/**
 	/**
-	 * 验证是否为汉字
+	 * 验证是否为汉字
 	 *
 	 *
 	 * @param value 值
 	 * @param value 值
 	 * @return 是否为汉字
 	 * @return 是否为汉字
 	 */
 	 */
 	public static boolean isChinese(CharSequence value) {
 	public static boolean isChinese(CharSequence value) {
-		return isMactchRegex("^" + ReUtil.RE_CHINESES + "$", value);
+		return isMatchRegex(PatternPool.CHINESES, value);
 	}
 	}
 
 
 	/**
 	/**

+ 6 - 0
hutool-core/src/test/java/cn/hutool/core/lang/ValidatorTest.java

@@ -136,4 +136,10 @@ public class ValidatorTest {
 		Assert.assertTrue(Validator.isPlateNumber("粤BA03205"));
 		Assert.assertTrue(Validator.isPlateNumber("粤BA03205"));
 		Assert.assertTrue(Validator.isPlateNumber("闽20401领"));
 		Assert.assertTrue(Validator.isPlateNumber("闽20401领"));
 	}
 	}
+
+	@Test
+	public void isChineseTest(){
+		Assert.assertTrue(Validator.isChinese("全都是中文"));
+		Assert.assertFalse(Validator.isChinese("not全都是中文"));
+	}
 }
 }