Java Regex String Replace replace(String source, String[] patterns, String[] replacements)

Here you can find the source of replace(String source, String[] patterns, String[] replacements)

Description

Search source for instances of patterns from patterns[].

License

Open Source License

Parameter

Parameter Description
patterns an array of regexes of what to find and what will be replaced. Note, that since they are regexes, you must be sure to escape special regex tokens.

Declaration

public static final String replace(String source, String[] patterns, String[] replacements) 

Method Source Code

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from ww  w . j  a  v  a  2s. co m
     * Search source for instances of patterns from patterns[].  If found, replace
     * with the corresponding replacement in replacements[] (i.e., pattern[n] is replaced by
     * replacement[n])
     *
     * @param patterns an array of regexes of what to find and what will be replaced. Note, that since they are
     *      regexes, you must be sure to escape special regex tokens.
     */
    public static final String replace(String source, String[] patterns, String[] replacements) {
        if (source == null)
            return null;
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < patterns.length; ++i) {
            if (i != 0)
                buf.append("|");
            buf.append("(").append(patterns[i]).append(")");
        }
        String regexString = buf.toString();
        Pattern regex = Pattern.compile(regexString);

        Matcher m = regex.matcher(source);
        if (m.groupCount() != replacements.length)
            throw new IllegalArgumentException("Mismatch between pattern and replacements array");

        StringBuffer result = null;
        int idx = 0;
        while (m.find()) {
            if (result == null)
                result = new StringBuffer(source.length() + 32);
            result.append(source.substring(idx, m.start()));
            for (int i = 0; i < replacements.length; ++i) {
                // the 0th group is the entire expression
                if (m.group(i + 1) != null) {
                    result.append(replacements[i]);
                    idx = m.end();
                    break;
                }
            }
        }
        if (result == null)
            return source;
        result.append(source.substring(idx));
        return result.toString();
    }
}

Related

  1. replace(String s, Properties p)
  2. replace(String s1, String s2, Pattern pattern)
  3. replace(String source, String prefix, String suffix, String prefixReplace, String suffixReplace)
  4. replace(String source, String search, String replace)
  5. replace(String source, String search, String replacement)
  6. replace(String str, String key, String value)
  7. replace(String str, String replacement, Function func)
  8. replace(String string, Pattern pattern, String replacement)
  9. replace(String string, Pattern[] patterns, String[] replacements)