Java Regex String Replace All replaceAll(Pattern p, String s, String r, Function cb)

Here you can find the source of replaceAll(Pattern p, String s, String r, Function cb)

Description

Replaces all occurrences of the pattern in this input

License

Open Source License

Parameter

Parameter Description
p The pattern to match on
s The input string
r The template of the replacement string
cb The callback that returns an array of objects to place into the template

Return

a string will all matched patterns replaced

Declaration

public static String replaceAll(Pattern p, String s, String r,
        Function<Matcher, Object[]> cb) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from  www  . j  a  v a  2s . co  m*/
     * Replaces all occurrences of the pattern in this input
     *
     * @param p The pattern to match on
     * @param s The input string
     * @param r The template of the replacement string
     * @param cb The callback that returns an array of objects to place into the
     *           template
     * @return a string will all matched patterns replaced
     */
    public static String replaceAll(Pattern p, String s, String r,
            Function<Matcher, Object[]> cb) {
        Matcher m = p.matcher(s);
        while (m.find()) {
            s = m.replaceFirst(String.format(r, cb.apply(m)));
            m = p.matcher(s);
        }

        return s;
    }
}

Related

  1. replaceAll(final String regex, final String replaceWith, final String subject)
  2. replaceAll(final String regex, final String text, final String replacement)
  3. replaceAll(Pattern pattern, String string, String replacement)
  4. replaceAll(String original, String regexWhat, String with)
  5. replaceAll(String regex, String ment, String str)
  6. replaceAll(String regex, String str, Map map)