是否可以反转对 String 的 replaceAll 方法的调用? [英] Is it possible to invert a call to String's replaceAll method?
本文介绍了是否可以反转对 String 的 replaceAll 方法的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如...
String s = "abcxyzabcxyz";
s.replaceAll("xyz", "---");
这通常会给我 "abc---abc---"
.我怎样才能让它给我 "---xyz---xyz"
呢?
This would normally give me "abc---abc---"
. How would I get it to give me "---xyz---xyz"
instead?
推荐答案
String#replaceAll
接受一个正则表达式,如果我理解正确,你可以使用:
String#replaceAll
accepts a regex, if I understood you correctly, you can use:
s.replaceAll("[^xyz]", "---")
说明:
[^xyz]
表示任何字符,不是x"、y"或z"之一.
[^xyz]
means any character that's not one of "x", "y" or "z".
为了将xyz"否定为一个系列,请使用:
In order to negate "xyz" as a series, use:
^(?!.*xyz).*$
这篇关于是否可以反转对 String 的 replaceAll 方法的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文