在Perl中
s/(.)1+/$1/g;
可以做到这一点,我假设如果Java具有与Perl兼容的正则表达式,它也应该可以工作。
编辑:这是什么意思
s { (.) # match any charater ( and capture it ) 1 # if it is followed by itself + # One or more times}{$1}gx; # And replace the whole things by the first captured character (with g modifier to replace all occurences)编辑:正如其他人指出的那样,Java中的语法将成为
original.replaceAll("(.)\1+", "$1");记住要逃避 1



