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 text, Map vars)
Replaces the variables present in the text and returns the result.
Each key will be added to the $ prefix.
return replace(text, vars, "$");
Stringreplace(CharSequence target, CharSequence replacement, String string)
replace
return Pattern.compile(quote(target.toString()) ).matcher(string)
        .replaceAll( quoteReplacement(replacement.toString()));
voidreplace(final String regex, final String replacement, final StringBuffer source, boolean all)
replace
Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (all) {
    matcher.replaceAll(replacement);
} else {
    matcher.replaceFirst(replacement);
Stringreplace(final String template, final Map map, final String prefix, final String suffix, boolean useMapForMatching)
A generic template replace function.
final StringBuilder patternDefinition = new StringBuilder();
if (prefix != null) {
    patternDefinition.append(createLiteralString(prefix));
patternDefinition.append("(");
if (useMapForMatching) {
    for (final Object key : map.keySet()) {
        patternDefinition.append(key);
...
Stringreplace(Matcher m, String rv, Object value)
replace
while (m.find()) {
    if (m.group(2) != null) {
        rv = rv.replace(m.group(0), String.format(m.group(2), value));
    } else {
        rv = rv.replace(m.group(0), String.valueOf(value));
return rv;
...
Stringreplace(Pattern pattern, String src, Function handler)
Replace string with pattern obtaining replacement values through handler function.
StringBuilder sb = null;
Matcher matcher = pattern.matcher(src);
int pos = 0;
while (matcher.find()) {
    if (sb == null) {
        sb = new StringBuilder();
    String expr = matcher.group();
...
Stringreplace(String content, String name, String value)
replace
Pattern pattern = Pattern.compile("[{]" + name + "[}]");
Matcher matcher = pattern.matcher(content);
StringBuffer buf = new StringBuffer();
int curpos = 0;
while (matcher.find()) {
    buf.append(content.substring(curpos, matcher.start()));
    curpos = matcher.end();
    buf.append(value);
...
Stringreplace(String input, Pattern pattern, Function replacementGenerator)
replace
StringBuffer sb = new StringBuffer();
Matcher m = pattern.matcher(input);
while (m.find()) {
    m.appendReplacement(sb, replacementGenerator.apply(m));
m.appendTail(sb);
return sb.toString();
Stringreplace(String input, Pattern regex, Function converter)
Finds and replaces matches from the input string using the regex regular expression and the converter replacement function.
Matcher matcher = regex.matcher(input);
StringBuilder sb = new StringBuilder();
int lastPosition = 0;
while (matcher.find()) {
    sb.append(input.substring(lastPosition, matcher.start()));
    sb.append(converter.apply(matcher.group()));
    lastPosition = matcher.end();
sb.append(input.substring(lastPosition));
return sb.toString();
Stringreplace(String inputStr, String patternStr, String replacementStr)
replace
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(inputStr);
String output = matcher.replaceAll(replacementStr);
return output;