举例:
"aaav.*sddff+ss" -> "av.*sdf+s"
代码:
//去重操作
String str = "aaav.*sddff+ss";
String regex = "(.)\1+";
Matcher matcher = Pattern.compile(regex).matcher(str);
String res = matcher.replaceAll("$1");
System.out.println(res);
主要用到正则表达式分组的概念。
1 用于正则表达式内取值,取的是第一个分组匹配到的值。
$1 用于正则表达式外取值, 取的是第一个分组匹配到的值。常用于replace方法。