1 public static void main(String[] args) throws Exception { 2 Pattern pattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"); 3 String text = "\"Joe 1\"55656kkk;[email protected]&56 <[email protected]>[email protected] Hello [email protected] is an e-mail..."; 4 Matcher matcher = pattern.matcher(text); 5 StringBuilder strBuilder = new StringBuilder(text); 6 while (matcher.find()) { 7 String subStr = matcher.group().replaceFirst(";", ""); 8 int subStrIndex = strBuilder.indexOf(subStr); 9 // 计算@字符位置 10 int keyWordIndex = subStr.indexOf("@"); 11 int asyKeyWordIndex = subStrIndex + keyWordIndex; 12 int leftIndex = asyKeyWordIndex - 3; 13 int rightIndex = asyKeyWordIndex + 4; 14 strBuilder.replace(leftIndex, asyKeyWordIndex, "***"); 15 strBuilder.replace(asyKeyWordIndex + 1, rightIndex, "***"); 16 } 17 System.out.println(strBuilder); 18 }View Code
标签:asyKeyWordIndex,java,int,matcher,strBuilder,日志,com,脱敏,gmail From: https://www.cnblogs.com/lljboke/p/17327975.html