Java Utililty Methods Regex String Replace

List of utility methods to do Regex String Replace

Description

The list of methods to do Regex String Replace are organized into topic(s).

Method

Stringreplace(String inString, String oldPattern, String newPattern)
replace
if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) {
    return inString;
StringBuilder sb = new StringBuilder();
int pos = 0; 
int index = inString.indexOf(oldPattern);
int patLen = oldPattern.length();
while (index >= 0) {
...
Stringreplace(String line, String regexp, String replacement)
replace
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    line = line.replace(matcher.group(), replacement);
return line;
Stringreplace(String message, ResourceBundle bundle)
replace
Matcher matcher = messagePattern.matcher(message);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
    String key = matcher.group(1);
    try {
        matcher.appendReplacement(sb, bundle.getString(key));
    } catch (MissingResourceException e) {
        matcher.appendReplacement(sb, key);
...
voidreplace(String operateOn[], String from, String to)
Iterates over the items in operateOn and replaces any matches of from to to.
for (int i = 0; i < operateOn.length; i++) {
    Pattern patternOutput = Pattern.compile(from);
    Matcher matcherOutput = patternOutput.matcher(operateOn[i]);
    operateOn[i] = matcherOutput.replaceFirst(to);
Stringreplace(String original, CharSequence target, CharSequence replacement)
Implementation according to JDK5 String.replace(CharSequence,CharSequence)
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(original)
        .replaceAll(quoteReplacement(replacement.toString()));
Stringreplace(String pattern, String replace, String s)
replace
return concatWithSeparator(explode(s, pattern).toArray(new String[0]), replace);
Stringreplace(String s, Pattern pattern, Function func)
replace
Matcher m = pattern.matcher(s);
if (m.find()) {
    StringBuilder sb = new StringBuilder((int) (s.length() * 1.5));
    int start = 0;
    do {
        if (start < m.start()) {
            sb.append(s, start, m.start());
        sb.append(func.apply(m));
        start = m.end();
    } while (m.find());
    if (start < s.length()) {
        sb.append(s, start, s.length());
    return sb.toString();
return s;
Stringreplace(String s, Properties p)
replace
String regex = "\\$\\w+\\W|\\$\\{[^}]+\\}";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(s);
while (m.find()) {
    String temp = m.group();
    String key = null;
    if (temp.indexOf("{") != -1) {
        key = temp.substring(temp.indexOf("{") + 1, temp.length() - 1);
...
Stringreplace(String s1, String s2, Pattern pattern)
replace
return pattern.matcher(s1).replaceAll(s2);
Stringreplace(String source, String prefix, String suffix, String prefixReplace, String suffixReplace)
replace
char[] chars = source.toCharArray();
char[] prefixChars = prefix.toCharArray();
char[] suffixChars = suffix.toCharArray();
StringBuffer result = new StringBuffer();
int index = 0;
boolean findPrefix = false;
boolean findSuffix = false;
int prefixIndex = -1;
...