比如你把 replaceAll("(.)\\1+","$1") 改成 replaceAll("(.)\\1+","$2")就会出现下面异常:
所以$1表示的就是匹配组1的内容;
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 2
at java.util.regex.Matcher.group(Matcher.java:470)
at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
at java.util.regex.Matcher.replaceAll(Matcher.java:813)
at java.lang.String.replaceAll(String.java:2190)
at test.Test.main(Test.java:20)