Class WordReplacer


  • public class WordReplacer
    extends Object
    • Constructor Detail

      • WordReplacer

        public WordReplacer()
    • Method Detail

      • replaceWords

        public static String replaceWords​(String input,
                                          Pattern pattern,
                                          Map<String,​String> replacements)
        Replace all matches in the input by their replacement. Matcher#appendReplacement is used and therefore `\` and `$` characters must be escaped in the replacement string. NOTE: To ignore casing, Pattern must be case-insensitive or contain all possible cases. And replacements must either be a case-insensitive map or have lowercase keys.
        Parameters:
        input - the text where the replacements take place
        pattern - the pattern
        replacements - the new words to use
      • replaceWords

        public static String replaceWords​(String input,
                                          Pattern pattern,
                                          Function<String,​String> replace)
        Replace all matches in the input by their replacement. Matcher#appendReplacement is used and therefore `\` and `$` characters must be escaped in the replacement string. NOTE: To ignore casing, Pattern must be case-insensitive or contain all possible cases.
        Parameters:
        input - the text where the replacements take place
        pattern - the pattern
        replace - the replace function. Accepts the matched word, and return the replacement
      • replaceWords

        public static String replaceWords​(String input,
                                          String[] words,
                                          String[] replaces)
        Replace all words in the input by the replaces. The replacements happens only if the texts to replace are not part of a greater word, that is, if the text is a whole word, separated by non Character.isLetterOrDigit(char) characters.
        Parameters:
        input - the text where the replacements take place
        words - the words to look for and replace
        replaces - the new words to use
      • replaceWordsIgnoreCase

        public static String replaceWordsIgnoreCase​(String input,
                                                    String[] words,
                                                    String[] replaces)
        Replace all words in the input by the replaces. The replacements happens only if the texts to replace are not part of a greater word, that is, if the text is a whole word, separated by non Character.isLetterOrDigit(char) characters.
        Parameters:
        input - the text where the replacements take place
        words - the words to look for and replace
        replaces - the new words to use
      • replaceWord

        public static String replaceWord​(String input,
                                         String word,
                                         String replace)
        Replace all occurrences of word by replace in the input. The replacement happens only if the word to replace is separated from others words, that is, it's not part of a word. The implementation is that the previous and next characters of the word must be a Character.isLetterOrDigit(char) char.
        Parameters:
        input - text where the replacements take place
        word - the word to replace
        replace - the new text to use
      • replaceWordIgnoreCase

        public static String replaceWordIgnoreCase​(String input,
                                                   String word,
                                                   String replace)
        Replace all occurrences of word by replace in the input. The replacement happens only if the word to replace is separated from others words, that is, it's not part of a word. The implementation is that the previous and next characters of the word must be a Character.isLetterOrDigit(char) char.
        Parameters:
        input - text where the replacements take place
        word - the word to replace
        replace - the new text to use
      • replaceWord

        public static void replaceWord​(StringBuilder input,
                                       String word,
                                       String replace)
        Replace all occurrences of word by replace in the input. The replacement happens only if the word to replace is separated from others words, that is, it's not part of a word. The implementation is that the previous and next characters of the word must be a Character.isLetterOrDigit(char) char.
        Parameters:
        input - text where the replacements take place
        word - the word to replace
        replace - the new text to use