Java Regex String Replace replace(String string, String pattern, String replacement, boolean literal)

Here you can find the source of replace(String string, String pattern, String replacement, boolean literal)

Description

Replaces all occurrences of the given regular expression in the given string with the given replacement.

License

Open Source License

Parameter

Parameter Description
string The string to be replaced.
pattern The regular expression pattern.
replacement The replacement string.
literal Whether the replacement string is literal and therefore requires quoting.

Return

The replaced string.

Declaration

public static String replace(String string, String pattern, String replacement, boolean literal) 

Method Source Code

//package com.java2s;
/*/*from ww  w .  ja  v  a  2s . c  o  m*/
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

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

public class Main {
    /**
     * Replaces all occurrences of the given regular expression in the given string with the given replacement.
     *
     * @param string      The string to be replaced.
     * @param pattern     The regular expression pattern.
     * @param replacement The replacement string.
     * @param literal     Whether the replacement string is literal and therefore requires quoting.
     * @return The replaced string.
     */
    public static String replace(String string, String pattern, String replacement, boolean literal) {
        return replace(string, pattern, replacement, literal, false);
    }

    /**
     * Replaces either the first or all occurrences of the given regular expression in the given string with the given
     * replacement.
     *
     * @param string      The string to be replaced.
     * @param pattern     The regular expression pattern.
     * @param replacement The replacement string.
     * @param literal     Whether the replacement string is literal and therefore requires quoting.
     * @param firstOnly   If true, only the first occurrence is replaced, otherwise all occurrences are replaced.
     * @return The replaced string.
     */
    public static String replace(String string, String pattern, String replacement, boolean literal,
            boolean firstOnly) {
        String output = string;
        if (string != null && pattern != null && replacement != null) {
            if (literal)
                replacement = Matcher.quoteReplacement(replacement);
            Matcher matcher = Pattern.compile(pattern).matcher(string);
            if (firstOnly) {
                output = matcher.replaceFirst(replacement);
            } else {
                output = matcher.replaceAll(replacement);
            }
        }
        return output;
    }
}

Related

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