Java - 字符串替换确切的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30183807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - String replace exact word
提问by Oti Na Nai
String x = "axe pickaxe";
x = x.replace("axe", "sword");
System.out.print(x);
By this code, I am trying to replace the exact word axe
with sword
. However, if I run this, it prints sword picksword
while I would like to print sword pickaxe
only, as pickaxe
is a different word from axe
although it contains it. How can I fix this? Thanks
通过这段代码,我试图axe
用sword
. 但是,如果我运行它,它sword picksword
会在我只想打印时打印sword pickaxe
,虽然它包含它pickaxe
,axe
但它是一个不同的词。我怎样才能解决这个问题?谢谢
采纳答案by hiergiltdiestfu
Use a regex with word boundaries\b
:
使用带有字边界的正则表达式\b
:
String s = "axe pickaxe";
System.out.println(s.replaceAll("\baxe\b", "sword"));
The backslash from the boundary symbol must be escaped, hence the double-backslashes.
边界符号的反斜杠必须被转义,因此是双反斜杠。
回答by SubOptimal
Include the word boundary before and after the word you want to replace.
在要替换的单词前后包括单词边界。
String x = "axe pickaxe axe";
x = x.replaceAll("\baxe\b", "sword");
System.out.print(x);
editoutput
编辑输出
sword pickaxe sword
回答by Steve Chaloner
You can use \b
to define the word boundary, so \baxe\b
in this case.
您可以使用\b
来定义单词边界,因此\baxe\b
在这种情况下。
x = x.replaceAll("\baxe\b");
回答by Veselin Davidov
System.out.println("axe pickaxe".replaceAll("\baxe\b", "sword"));
You need to use replaceAll instead of replace - because it can work with regular expressions. Then use the meta character \b which is for word boundary. In order to use it you need to escape the \ as double \ so the reges become \\baxe\\b
您需要使用 replaceAll 而不是 replace - 因为它可以使用正则表达式。然后使用元字符 \b 用于单词边界。为了使用它,你需要将 \ 转义为双 \ 所以reges 变成\\baxe\\b
回答by Avinash Raj
Some other answers suggest you to use \\b
word boundaries to match an exact word. But \\bfoo\\b
would match the substring foo
in .foo.
. If you don't want this type of behavior then you may consider using lookaround based regex.
其他一些答案建议您使用\\b
单词边界来匹配精确的单词。但是,\\bfoo\\b
将匹配子foo
在.foo.
。如果您不想要这种类型的行为,那么您可以考虑使用基于环视的正则表达式。
System.out.println(string.replaceAll("(?<!\S)axe(?!\S)", "sword"));
Explanation:
解释:
(?<!\\S)
Negative lookbehind which asserts that the match won't be preceded by a non-space character. i.e. the match must be preceded by start of the line boundary or a space.(?!\\S)
Negative lookahead which asserts that the match won't be followed by a non-space character, i.e. the match must be followed by end of the line boundary or space. We can't use(?<=^|\\s)axe(?=\\s|$)
since Java doesn't support variable length lookbehind assertions.
(?<!\\S)
否定的lookbehind 断言匹配不会以非空格字符开头。即匹配必须以行边界或空格的开头开头。(?!\\S)
否定前瞻,断言匹配后不会跟随非空格字符,即匹配后必须是行边界或空格的结尾。我们不能使用,(?<=^|\\s)axe(?=\\s|$)
因为 Java 不支持可变长度的后视断言。
回答by Alvin Magalona
If you only want to replace the first occurrence, there is a method replaceFirst
in String
object.
如果你只想要替换第一次出现,是有方法replaceFirst
的String
对象。
String x = "axe pickaxe";
x = x.replaceFirst("axe", "sword");
System.out.print(x); //returns sword pickaxe