Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用正则表达式替换字符串中出现的所有单词_Java_Regex_String_Replace - Fatal编程技术网

Java 使用正则表达式替换字符串中出现的所有单词

Java 使用正则表达式替换字符串中出现的所有单词,java,regex,string,replace,Java,Regex,String,Replace,是否没有简单的方法来替换字符串中出现的所有(整个)单词?我目前正在使用这个,它不是很优雅: public static String replace(String input, String toReplace, String replacement){ if(input==null) throw new NullPointerException(); input = input.replace(" "+toReplace+

是否没有简单的方法来替换字符串中出现的所有(整个)单词?我目前正在使用这个,它不是很优雅:

public static String replace(String input, String toReplace, 
                           String replacement){
    if(input==null) throw new NullPointerException();
    input = input.replace(" "+toReplace+" ", " "+replacement+" ");
    input = input.replaceAll("^"+toReplace+" ", replacement+" ");
    input = input.replaceAll(" "+toReplace+"$", " "+replacement);
    return input;
}
此外,正则表达式
“^”+toReplace+”
不是正则表达式安全的。例如:当它可能包含像
[
等)这样的字符时

编辑:

此代码的任何原因:

public static String replace(String input, String toReplace, 
                           String replacement){
    if(input==null) throw new NullPointerException();
    input = input.replace(" "+toReplace+" ", " "+replacement+" ");
    input = input.replaceAll(Pattern.quote("^"+toReplace+" "), replacement+" ");
    input = input.replaceAll(Pattern.quote(" "+toReplace+"$"), " "+replacement);
    //input = input.replaceAll("\\b" + Pattern.quote(toReplace) + "\\b", replacement);
    return input;
}
在以下情况下,其行为方式如下:

    input = "test a testtest te[(st string test";
    input = replace(input, toReplace, "REP");
    System.out.println(input);
a)
toReplace=test
打印:

test a testtest te[(st string test
test a testtest REP string test
b)
toReplace=te[(st
打印:

test a testtest te[(st string test
test a testtest REP string test

谢谢,

使用单词边界
\b
模式。引用
进行转义

return input.replaceAll("\\b" + Pattern.quote(toReplace) + "\\b", replacement);

\\b
表示的是单词和非单词字符之间的零宽度边界,包括字符串的最开头和最结尾。

单词边界有一个特殊的regexp代码-
\b
。它包括手动处理空格/行尾开头以及标点符号等其他情况

有一个方法
Pattern.quote()
引用字符串以保护regexp special,正如您所建议的,如果字符串是任意的或可能是用户提供的,则应始终使用该方法

因此,可以得出:

input.replaceAll("\\b"+Pattern.quote(toReplace)+"\\b", replacement);
\b匹配单词边界,请参见


使用java.util.regex.Pattern.quote对特殊字符进行转义。

您需要了解regex
\b
,它是“字边界”的零宽度匹配。使用它,您的方法只需一行:

return input.replaceAll("\\b"+Pattern.quote(toReplace)+"\\b", replacement);

你有什么问题?你的期望是什么?你得到了什么?我有两个期望:a)我们应该替换regex safe。b)代码的经济性(可能是一行代码).regex安全是什么意思?如果你不想在你的regex模式中使用regex特殊字符,为什么要使用regex?使用用户输入字符串作为regex模式是一个标志,表明你应该重新考虑你的方法。不需要捕获和重写
\\b
,它们是零长度的。不捕获?他把它们放在paren中,使em捕获的组。@Keppil啊,你的意思是零宽度,不是非捕获。说得好。谢谢,我有一个附带问题。如果我替换input=input。替换(“^”+torerece+”,“+replacement+”);替换为input=input.replace(Pattern.quote(“^”+torerece+”,“+replacement+”);它不适用于测试用例:StringUtils.replace(input,“test”,“REP”);输入=测试te[(st字符串。预期输出=代表测试te[(st字符串string@aryan你是想在那里再键入一些吗?代码失败;你必须在regex.Missing\fixed.Thx中避开反斜杠以获取注释。你需要在那里填充.quote(toReplace)。@aryan…这就是我的意思“使用java.util.regex.Pattern.quote转义特殊字符。”-但您是对的,它也应该在代码中。