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

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

Description

Replaces all occurrences of several regular expressions in order.

License

LGPL

Parameter

Parameter Description
string The string
patterns An array of regular expressions
replacements An array of replacement strings (must be the same length and order as patterns)

Return

The resulting string

Declaration

public static String replace(String string, Pattern[] patterns, String[] replacements) 

Method Source Code

//package com.java2s;
/**/*from ww  w.ja  va  2s.  c  o  m*/
 * Copyright 2009-2012 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.util.regex.Pattern;

public class Main {
    /**
     * Replaces all occurrences of several regular expressions in order.
     * 
     * @param string
     *        The string
     * @param patterns
     *        An array of regular expressions
     * @param replacements
     *        An array of replacement strings (must be the same length and order
     *        as patterns)
     * @return The resulting string
     */
    public static String replace(String string, Pattern[] patterns, String[] replacements) {
        for (int i = 0, length = patterns.length; i < length; i++)
            string = patterns[i].matcher(string).replaceAll(replacements[i]);
        return string;
    }
}

Related

  1. replace(String source, String search, String replacement)
  2. replace(String source, String[] patterns, String[] replacements)
  3. replace(String str, String key, String value)
  4. replace(String str, String replacement, Function func)
  5. replace(String string, Pattern pattern, String replacement)
  6. replace(String string, Properties table)
  7. replace(String string, String pattern, String replacement, boolean literal)
  8. replace(String text, Map vars, String prefix)
  9. replace(String text, String find, String match, boolean useRegex, boolean isCaseSensitive)