Java 正则表达式用一个破折号替换2个或多个破折号 我使用了许多正则表达式,但是问题是我也要考虑: abc- -abc //should output abc-abc abc -- abc //should output abc - abc abc- - abc //should output abc- abc abc - -abc //should output abc -abc
我使用了:Java 正则表达式用一个破折号替换2个或多个破折号 我使用了许多正则表达式,但是问题是我也要考虑: abc- -abc //should output abc-abc abc -- abc //should output abc - abc abc- - abc //should output abc- abc abc - -abc //should output abc -abc,java,regex,string,replaceall,Java,Regex,String,Replaceall,我使用了: String x=x.replaceAll("[\\-*]{2,}","-"); 可以使用以下正则表达式: -(\\s*-)+ -:按字面意思匹配- (…)+:分组(1次以上) \\s*-:匹配-可选的空格(\s) x = x.replaceAll("-(\\s*-)+", "-");
String x=x.replaceAll("[\\-*]{2,}","-");
可以使用以下正则表达式:
-(\\s*-)+
:按字面意思匹配-
-
:分组(1次以上)(…)+
:匹配\\s*-
可选的空格(-
)\s
x = x.replaceAll("-(\\s*-)+", "-");