Class WordReplacer

java.lang.Object
com.cloudbees.jenkins.support.util.WordReplacer

@Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public class WordReplacer extends Object
  • Constructor Details

    • WordReplacer

      public WordReplacer()
  • Method Details

    • replaceWordsByOffset

      public static String replaceWordsByOffset(String input, String normalized, Pattern pattern, Function<String,String> replace)
      Replace all matches in the normalized text by splicing replacements into the original input at matching offsets. The pattern is matched against normalized, but replacements are spliced into input using the same offsets. This is safe when normalization preserves offset alignment (never changes charCount(cp)).

      Unlike replaceWords(String, Pattern, Function), this method does not use Matcher#appendReplacement, so \ and $ in replacements do not need escaping.

      Parameters:
      input - the original text, returned unchanged if no matches
      normalized - the case-normalized copy of input, matched against the pattern
      pattern - the pattern to match (should have no case-insensitive flags since normalized is pre-normalized)
      replace - function accepting a matched substring from normalized and returning its replacement
      Returns:
      input itself when no matches; otherwise a new string with replacements spliced in
    • 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