一个使用正则表达式治口吃的问题。将“我我我...我我.要要要...要...学学学...编编编...程”抽取为“我要学编程”
import java.util.TreeSet;
public class RegexTest {
public static void main(String[] args) {
Method1();//1.治口吃
}
//1.治口吃
private static void Method1() {
String str="我我我...我我.要要要...要...学学学...编编编...程";
//1.将字符串.去掉。使用替换
str=str.replaceAll("\\.", "");
System.out.println(str);
//2.替换叠词。使用组
str=str.replaceAll("(.)\\1+", "$1");//将每组中的第一个输出
System.out.println(str);
}
}
运行结果: