我想"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'
中,用匹配的字符C
或c
(使用反向引用)替换字符串。我不知道在 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"