1

我想"Cannot""Can not""cannot"替换字符串"can not"。为此,我使用下面的代码:

 String string = "I Cannot do it.";
 string = string.replaceAll("([Cc])annot", "\\1an not");

所需的字符串是"I Can not do it.".

String string = "I Cannot do it.";
string = string.replaceAll("([Cc])annot", "\\1an not");

所需的字符串是"I can not do it". 在 Ruby'\1'中,用匹配的字符Cc(使用反向引用)替换字符串。我不知道在 Java 中使用什么。下面是运行良好的 Ruby 正则表达式:

"I Cannot do it".gsub!(/([Cc])annot/,'\1an not')
# => "I Can not do it"       
"I cannot do it".gsub!(/([Cc])annot/,'\1an not')
# => "I can not do it"
4

2 回答 2

5

关于什么

String string = "I Cannot do it."
string = string.replaceAll("([Cc])annot","$1an not");
于 2013-09-19T07:45:03.250 回答
4

试着说:

string = string.replaceAll("([Cc])annot","$1an not");
于 2013-09-19T07:45:08.357 回答