|
@@ -24,6 +24,14 @@ public final class MobileUtils {
|
|
|
*/
|
|
|
public static final String DEFAULT_DESENSITIZE_EXPRESSION = "---****-?";
|
|
|
|
|
|
+ /**
|
|
|
+ * 脱敏手机号默认匹配模式
|
|
|
+ */
|
|
|
+ private static final Pattern DEFAULT_DESENSITIZE_PATTERN = Pattern.compile(
|
|
|
+ "[1一壹][^0-9\\u4e00-\\u9fa5]*[3-9零〇一壹二贰弍三叁弎仨四肆五伍六陆七柒八捌九玖]" +
|
|
|
+ "([^0-9\\u4e00-\\u9fa5]*[0-9零〇一壹二贰弍三叁弎仨四肆五伍六陆七柒八捌九玖]){9}"
|
|
|
+ );
|
|
|
+
|
|
|
/**
|
|
|
* 手机号处理工具实例
|
|
|
*/
|
|
@@ -34,12 +42,6 @@ public final class MobileUtils {
|
|
|
*/
|
|
|
private static final PhoneNumberOfflineGeocoder GEOCODER = PhoneNumberOfflineGeocoder.getInstance();
|
|
|
|
|
|
- /**
|
|
|
- * 手机号脱敏匹配模式
|
|
|
- */
|
|
|
- private static final Pattern DESENSITIZE_MOBILE_PATTERN
|
|
|
- = Pattern.compile("1[\\.\\-—~~_ ]*[3-9]([\\.\\-—~~_ ]*[0-9]){9}");
|
|
|
-
|
|
|
private MobileUtils() {
|
|
|
}
|
|
|
|
|
@@ -97,7 +99,7 @@ public final class MobileUtils {
|
|
|
* @return true/false
|
|
|
*/
|
|
|
public static boolean isMobile(String mobile, @NonNull Locale locale) {
|
|
|
- return StringUtils.notEmpty(mobile) && mobile.length() == 11 && isMobile(parse(mobile, locale));
|
|
|
+ return isMobile(parse(mobile, locale));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -152,6 +154,17 @@ public final class MobileUtils {
|
|
|
return desensitize(text, DEFAULT_DESENSITIZE_EXPRESSION);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 脱敏手机号
|
|
|
+ *
|
|
|
+ * @param text 文本内容
|
|
|
+ * @param pattern 手机号匹配模式
|
|
|
+ * @return 文本内容
|
|
|
+ */
|
|
|
+ public static String desensitize(String text, @NonNull Pattern pattern) {
|
|
|
+ return desensitize(text, pattern, DEFAULT_DESENSITIZE_EXPRESSION);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 脱敏手机号
|
|
|
*
|
|
@@ -160,6 +173,18 @@ public final class MobileUtils {
|
|
|
* @return 文本内容
|
|
|
*/
|
|
|
public static String desensitize(String text, @NonNull String expression) {
|
|
|
+ return desensitize(text, DEFAULT_DESENSITIZE_PATTERN, expression);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 脱敏手机号
|
|
|
+ *
|
|
|
+ * @param text 文本内容
|
|
|
+ * @param pattern 手机号匹配模式
|
|
|
+ * @param expression 手机号脱敏模式表达式
|
|
|
+ * @return 文本内容
|
|
|
+ */
|
|
|
+ public static String desensitize(String text, @NonNull Pattern pattern, @NonNull String expression) {
|
|
|
if (StringUtils.isEmpty(text)) {
|
|
|
return text;
|
|
|
}
|
|
@@ -167,7 +192,7 @@ public final class MobileUtils {
|
|
|
int offset = 0;
|
|
|
StringBuilder buffer = null;
|
|
|
DesensitizeUtils.Mode mode = null;
|
|
|
- Matcher matcher = DESENSITIZE_MOBILE_PATTERN.matcher(text);
|
|
|
+ Matcher matcher = pattern.matcher(text);
|
|
|
while (matcher.find()) {
|
|
|
if (buffer == null) {
|
|
|
buffer = new StringBuilder();
|
|
@@ -180,6 +205,9 @@ public final class MobileUtils {
|
|
|
if (mobile.length() > 11) {
|
|
|
mobile = StringUtils.clear(mobile);
|
|
|
}
|
|
|
+ if (ChineseUtils.isChineseNumber(mobile)) {
|
|
|
+ mobile = ChineseUtils.Number.format(mobile);
|
|
|
+ }
|
|
|
buffer.append(mode.desensitize(mobile));
|
|
|
offset = matcher.end();
|
|
|
}
|