可以将正则表达式修改为"(?<=.).(?=.)"来匹配@左右两边的两个字符。修改后的代码如下:

public static void main(String[] args) { String email = "pfl0611121333@3.com"; String encryptedEmail = encryptEmail(email); System.out.println(encryptedEmail); }

public static String encryptEmail(String email) { Pattern pattern = Pattern.compile("(?<=.).(?=.)"); Matcher matcher = pattern.matcher(email);

// 将匹配到的字符替换为*
StringBuffer encryptedEmail = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(encryptedEmail, "*");
}
matcher.appendTail(encryptedEmail);

return encryptedEmail.toString();
public static void mainString args String email = pfl06111213333com; String encryptedEmail = encryptEmailemail; SystemoutprintlnencryptedEmail; public static String encryp

原文地址: http://www.cveoy.top/t/topic/hNBH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录