在java中,可以使用哪个正则表达式来替换这些表达式,例如:
前:aabbb 后:AB
前:14442345后:142345
谢谢
发布于 2017-12-27 09:46:57
在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”
发布于 2017-12-27 10:02:43
String a = "aaabbb";
String b = a.replaceAll("(.)\\1+", "$1");
System.out.println("'" + a + "' -> '" + b + "'");
发布于 2017-12-27 11:32:11
匹配模式(在必须转义的Java/语言中):
(.)\\1+
或者(在语言中,您可以使用不将\作为转义字符的字符串)
(.)\1+
更换*
$1
https://stackoverflow.com/questions/-100000030
复制相似问题