Replace specific string by another - String#replaceAll()

独自空忆成欢 提交于 2019-12-31 04:32:08

问题


I'm actually developping a parser and I'm stuck on a method.

I need to clean specifics words in some sentences, meaning replacing those by a whitespace or a nullcharacter. For now, I came up with this code:

private void clean(String sentence)
{
    try {
        FileInputStream fis = new FileInputStream(
                ConfigHandler.getDefault(DictionaryType.CLEANING).getDictionaryFile());
        BufferedReader bis = new BufferedReader(new InputStreamReader(fis));
        String read;
        List<String> wordList = new ArrayList<String>();

        while ((read = bis.readLine()) != null) {
            wordList.add(read);
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    for (String s : wordList) {
        if (StringUtils.containsIgnoreCase(sentence, s)) { // this comes from Apache Lang
            sentence = sentence.replaceAll("(?i)" + s + "\\b", " ");
        }
    }

    cleanedList.add(sentence);

} 

But when I look at the output, I got all of the occurences of the word to be replaced in my sentence replaced by a whitespace.

Does anybody can help me out on replacing only the exact words to be replaced on my sentence?

Thanks in advance !


回答1:


There are two problems in your code:

  • You are missing the \b before the string
  • You will run into issues if any of the words from the file has special characters

To fix this problem construct your regex as follows:

sentence = sentence.replaceAll("(?i)\\b\\Q" + s + "\\E\\b", " ");

or

sentence = sentence.replaceAll("(?i)\\b" + Pattern.quote(s) + "\\b", " ");


来源:https://stackoverflow.com/questions/35889377/replace-specific-string-by-another-stringreplaceall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!