在java中,可以使用哪个正则表达式来替换它们,例如:
之前:aaabbb之后:ab
之前:14442345之后:142345
谢谢!
在perl
s/(.)\1+/$1/g;
诀窍,我假设如果java有perl兼容的regexp它也应该工作.
编辑:这就是它的含义
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
String a = "aaabbb"; String b = a.replaceAll("(.)\\1+", "$1"); System.out.println("'" + a + "' -> '" + b + "'");