Java Regex String Replace replace(String text, String findPattern, String replacePattern)

Here you can find the source of replace(String text, String findPattern, String replacePattern)

Description

Replace any text within a text using regular expressions

License

Apache License

Parameter

Parameter Description

Declaration

public static String replace(String text, String findPattern, String replacePattern) 

Method Source Code


//package com.java2s;
/*/* www  .ja v a  2 s .  com*/
 * EBI MetaboLights - http://www.ebi.ac.uk/metabolights
 * Cheminformatics and Metabolism group
 *
 * European Bioinformatics Institute (EMBL-EBI), European Molecular Biology Laboratory, Wellcome Trust Genome Campus, Hinxton, Cambridge CB10 1SD, United Kingdom
 *
 * Last modified: 9/9/13 4:37 PM
 * Modified by:   conesa
 *
 *
 * ?, EMBL, European Bioinformatics Institute, 2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * Replace any text within a text using regular expressions
     * @param text: Text to search
     * @param findPattern: Text to find (regular expression)
     * @param replacePattern: Text to replace (regular expression)
     * @return
     */
    public static String replace(String text, String findPattern, String replacePattern) {

        //Create the pattern for the search
        Pattern pattern = Pattern.compile(findPattern);

        // Replace all
        Matcher matcher = pattern.matcher(text);

        //Return
        return matcher.replaceAll(replacePattern);

    }
}

Related

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