使用java replaceAll方法用一个正则表达式替换句子中的单词

A-l*_*bby 1 java regex replace str-replace

我试图用字符串中的"is not"替换"is"但是有一个例外,它不应该替换驻留在其他单词中的"is".

"This is an ant" --> "This is not an ant" [CORRECT]
"This is an ant" --> "This not is not an ant" [INCORRECT]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我所做的是

String result = str.replaceAll("([^a-zA-Z0-9])is([^a-zA-Z0-9])","$1is not$2");
result = result.replaceAll("^is([^a-zA-Z0-9])","is not$1");
result = result.replaceAll("([^a-zA-Z0-9])is$","$1is not");
result = result.replaceAll("^is$","is not");
Run Code Online (Sandbox Code Playgroud)

但我认为只有一个正则表达式是可能的,但我无法弄明白.可能吗?

fal*_*tru 5

使用单词边界(\b):

result = str.replaceAll("\\bis\\b", "is not");
Run Code Online (Sandbox Code Playgroud)

注意:\应该被转义.否则它匹配退格(U + 0008).

演示.