Java Utililty Methods Regex String Replace All

List of utility methods to do Regex String Replace All

Description

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

Method

StringreplaceAll(String str, String regex, String replacement)
Enhances the basic String#replaceAll method by including the Pattern.DOTALL bitmask in the replacement operation.
StringBuffer sb = new StringBuffer();
try {
    Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        matcher.appendReplacement(sb, replacement);
    matcher.appendTail(sb);
...
StringreplaceAll(String str, String regex, String replacement)
replace All
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    m.appendReplacement(sb, replacement);
m.appendTail(sb);
return sb.toString();
...
StringreplaceAll(String string, Pattern pattern, String repalStr)
replace All
if (string == null) {
    return null;
Matcher m = pattern.matcher(string);
if (m.find()) {
    StringBuffer sb = new StringBuffer();
    while (true) {
        m.appendReplacement(sb, repalStr);
...
StringreplaceAll(String string, Pattern[] patterns, String replacement)
replace All
if (string == null || string.length() == 0) {
    return string;
if (patterns != null) {
    for (Pattern pattern : patterns) {
        string = pattern.matcher(string).replaceAll(replacement);
return string;
StringreplaceAll(String target, String from, String to)
Replaces the all substrings of this string that matches the given from string with the given replacement.
return target.replaceAll(Pattern.quote(from), Matcher.quoteReplacement(to));
StringreplaceAll(String text, Pattern pattern, String replace)
replace All
Matcher m = pattern.matcher(text);
StringBuffer sb = null;
while (m.find()) {
    if (sb == null)
        sb = new StringBuffer();
    m.appendReplacement(sb, replace);
if (sb != null)
...
StringBuilderreplaceAll(StringBuilder sb, String regex, String replacement)
The equivalent of String.replaceAll() for StringBuilder
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(sb);
int start = 0;
while (m.find(start)) {
    sb.replace(m.start(), m.end(), replacement);
    start = m.start() + replacement.length();
return sb;
...
StringreplaceAllBackreference(String text, String regex, String replacement)
replace All Backreference
Matcher m = createMatcher(text, regex);
text = m.replaceAll(replacement);
return text;
StringreplaceAllIgnoreCase(String source, String oldstring, String newstring)
replace All Ignore Case
Pattern p = Pattern.compile(oldstring, 34);
Matcher m = p.matcher(source);
return m.replaceAll(newstring);
StringreplaceAllLiteral(@Nonnull String value, @Nonnull Pattern pattern, @Nonnull String replace)
replace All Literal
return pattern.matcher(value).replaceAll(Matcher.quoteReplacement(replace));