Replace specific string by another - String#replac

2020-04-18 09:18发布

问题:

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", " ");