//$1、 $2、……表示正则表达式里面第一个括号和第二个括号里面的匹配内容
String sendPhoneStar = “13800007636”.replaceAll("(\d{3})\d{4}(\d{4})", “$1****$2”);
System.out.println(sendPhoneStar); // 138****7636
案例:
public class TestMain {
public static void main(String[] args) {
//$1、 $2、……表示正则表达式里面第一个括号和第二个括号里面的匹配内容
String sendPhoneStar = "13800007636".replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
System.out.println(sendPhoneStar); // 138****7636
String sendPhoneStar1 = "13800007636".replaceAll("(\\d{3})\\d{4}(\\d{4})", "1****2");
System.out.println("不加$符号 : "+sendPhoneStar1); //不加$符号 : 1****2
String sendPhoneStar2 = "13800007636".replaceAll("(\\d{3})(\\d{4})(\\d{4})", "$1****$2");
System.out.println("加括号 : "+sendPhoneStar2); //加括号 : 138****9388
String sendPhoneStar3 = "13800007636".replaceAll("(\\d{3})(\\d{4})(\\d{4})", "$1****$2****$3");
System.out.println(sendPhoneStar3); //138****0000****7636
String sendPhoneStar4 = "13800007636".replaceAll("(\\d{3})(\\d{4})(\\d{4})", "$1****$2***10");
System.out.println(sendPhoneStar4); //138****0000***10
String sendPhoneStar5 = "13800007636".replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****");
System.out.println(sendPhoneStar5); //138****
}
}