对字符串敏感信息脱敏(简单的正则)
public static void main(String[] args) {
String str = "我是一只小小鸟".replaceAll("(?<=.{1}).(?=.{1})", "*");
System.out.println(str);
String str1 = getDesensitizationPhoneStr("java学习19888166266我们是认真的");
System.out.println(str1);
}
public static String getDesensitizationPhoneStr(String phoneStr) {
if (StringUtils.isEmpty(phoneStr)) {
return phoneStr;
}
Pattern p = Pattern.compile("1[3456789]\\d{9}");
Matcher m = p.matcher(phoneStr);
while (m.find()) {
String group = m.group();
String s = group.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
phoneStr = phoneStr.replace(group, s);
}
return phoneStr;
}