其实使用的就是String类中的replaceAll()方法。
public class RegexDemo { public static void main(String[] args) { functionDemo(); } public static void functionDemo() { String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu"; str = str.replaceAll("(.)\1+", "$1"); System.out.println(str); String tel = "15800001111";//158****1111; tel = tel.replaceAll("(\d{3})\d{4}(\d{4})", "$1****$2"); System.out.println(tel); } }