Java Regex String Replace All replaceAll(StringBuilder sb, String regex, String replacement)

Here you can find the source of replaceAll(StringBuilder sb, String regex, String replacement)

Description

The equivalent of String.replaceAll() for StringBuilder

License

Apache License

Declaration

public static StringBuilder replaceAll(StringBuilder sb, String regex, String replacement) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.*;

public class Main {
    /**//from www  . j a v  a2s .c o  m
     * The equivalent of String.replaceAll() for StringBuilder
     */
    public static StringBuilder replaceAll(StringBuilder sb, String regex, String replacement) {
        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;
    }
}

Related

  1. replaceAll(String str, String regex, String replacement)
  2. replaceAll(String string, Pattern pattern, String repalStr)
  3. replaceAll(String string, Pattern[] patterns, String replacement)
  4. replaceAll(String target, String from, String to)
  5. replaceAll(String text, Pattern pattern, String replace)
  6. replaceAllBackreference(String text, String regex, String replacement)
  7. replaceAllIgnoreCase(String source, String oldstring, String newstring)
  8. replaceAllLiteral(@Nonnull String value, @Nonnull Pattern pattern, @Nonnull String replace)
  9. replaceAllPerLine(Pattern pattern, String value, String replacement)