我想将"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"
答案 0 :(得分:5)
怎么样?
String string = "I Cannot do it."
string = string.replaceAll("([Cc])annot","$1an not");
答案 1 :(得分:4)
试着说:
string = string.replaceAll("([Cc])annot","$1an not");