Java Regex String Replace All replaceAll(String regularExpression, String string, String newValue)

Here you can find the source of replaceAll(String regularExpression, String string, String newValue)

Description

replace All

License

Open Source License

Declaration

public static String replaceAll(String regularExpression, String string, String newValue) 

Method Source Code


//package com.java2s;
/*// w w  w. j  a  v a  2 s  .co m
 * Demoiselle Framework
 * Copyright (C) 2013 SERPRO
 * ----------------------------------------------------------------------------
 * This file is part of Demoiselle Framework.
 * 
 * Demoiselle Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License version 3
 * along with this program; if not,  see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 * ----------------------------------------------------------------------------
 * Este arquivo ? parte do Framework Demoiselle.
 * 
 * O Framework Demoiselle ? um software livre; voc? pode redistribu?-lo e/ou
 * modific?-lo dentro dos termos da GNU LGPL vers?o 3 como publicada pela Funda??o
 * do Software Livre (FSF).
 * 
 * Este programa ? distribu?do na esperan?a que possa ser ?til, mas SEM NENHUMA
 * GARANTIA; sem uma garantia impl?cita de ADEQUA??O a qualquer MERCADO ou
 * APLICA??O EM PARTICULAR. Veja a Licen?a P?blica Geral GNU/LGPL em portugu?s
 * para maiores detalhes.
 * 
 * Voc? deve ter recebido uma c?pia da GNU LGPL vers?o 3, sob o t?tulo
 * "LICENCA.txt", junto com esse programa. Se n?o, acesse <http://www.gnu.org/licenses/>
 * ou escreva para a Funda??o do Software Livre (FSF) Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
 */

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

public class Main {
    public static String replaceAll(String regularExpression, String string, String newValue) {
        List<String> matches = new ArrayList<String>();
        Pattern pattern = Pattern.compile(regularExpression);
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            matches.add(matcher.group(1));
        }
        for (String item : matches) {
            string = string.replace(item, newValue);
        }
        return string;
    }
}

Related

  1. replaceAll(Pattern p, String s, String r, Function cb)
  2. replaceAll(Pattern pattern, String string, String replacement)
  3. replaceAll(String original, String regexWhat, String with)
  4. replaceAll(String regex, String ment, String str)
  5. replaceAll(String regex, String str, Map map)
  6. replaceAll(String source, Pattern pattern, String replace)
  7. replaceAll(String str, String originalToken, String replacementToken)
  8. replaceAll(String str, String regex, String replacement)
  9. replaceAll(String str, String regex, String replacement)