Java Regex String Replace All replaceAll(String str, String regex, String replacement)

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

Description

Enhances the basic String#replaceAll method by including the Pattern.DOTALL bitmask in the replacement operation.

License

Open Source License

Declaration

public static String replaceAll(String str, String regex, String replacement) 

Method Source Code


//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from ww w.ja  v a 2s .  com
     * Enhances the basic String#replaceAll method by including the Pattern.DOTALL bitmask in the replacement operation.
     */
    public static String replaceAll(String str, String regex, String replacement) {
        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);
        } catch (Exception ex) {
        }
        return sb.toString();
    }
}

Related

  1. replaceAll(String regex, String ment, String str)
  2. replaceAll(String regex, String str, Map map)
  3. replaceAll(String regularExpression, String string, String newValue)
  4. replaceAll(String source, Pattern pattern, String replace)
  5. replaceAll(String str, String originalToken, String replacementToken)
  6. replaceAll(String str, String regex, String replacement)
  7. replaceAll(String string, Pattern pattern, String repalStr)
  8. replaceAll(String string, Pattern[] patterns, String replacement)
  9. replaceAll(String target, String from, String to)