Here you can find the source of replace(String template, Map
public static String replace(String template, Map<String, String> funcAndDic)
//package com.java2s; //License from project: Open Source License import java.util.Map; import java.util.Map.Entry; public class Main { private static final String PRE_VAR = "$Var{"; public static String replace(String template, Map<String, String> funcAndDic) { if (funcAndDic == null) { return template; }//from w ww .ja va2s. c o m for (Entry<String, String> entry : funcAndDic.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); String[] items = key.split("-"); String replaceStr = items[1].replaceAll("\\$", "\\\\\\$").replaceAll("\\{", "\\\\\\{") .replaceAll("\\}", "\\\\\\}").replaceAll("\\(", "\\\\\\(").replaceAll("\\)", "\\\\\\)"); value = value.replaceAll("\\$", "\\\\\\$").replaceAll("\\{", "\\\\\\{").replaceAll("\\}", "\\\\\\}") .replaceAll("\\(", "\\\\\\(").replaceAll("\\)", "\\\\\\)"); if (items[1].startsWith(PRE_VAR)) { //System.out.println(items[1] + "+++++++++++++++++++" + replaceStr); template = template.replaceAll(replaceStr + "=", ""); template = template.replaceAll(replaceStr, value); } else { template = template.replaceFirst(replaceStr, value); } } funcAndDic.clear(); funcAndDic = null; return template; } }