Here you can find the source of replace(String input, Pattern pattern, Function
public static String replace(String input, Pattern pattern, Function<Matcher, String> replacementGenerator)
//package com.java2s; //License from project: Apache License import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String replace(String input, Pattern pattern, Function<Matcher, String> replacementGenerator) { StringBuffer sb = new StringBuffer(); Matcher m = pattern.matcher(input); while (m.find()) { m.appendReplacement(sb, replacementGenerator.apply(m)); }/*from w w w . jav a 2s. c om*/ m.appendTail(sb); return sb.toString(); } }