java 替换 ${xxx} 的内容
public static void main(String[] args) {
Map<String,String> mm=new HashMap<>();
mm.put("${a}","han");
mm.put("${b}","zhong");
System.out.println( parse("例如有这样一个${a}字符串字符串:用户'${a}'的名称${b}", mm));
}
private static Pattern p= Pattern.compile("(\\$\\{(.*?)})");
private static String parse(String content,Map<String,String> kvs){
Matcher m = p.matcher(content);
StringBuffer sr = new StringBuffer();
while(m.find()){
String group = m.group();
m.appendReplacement(sr, kvs.get(group));
}
m.appendTail(sr);
return sr.toString();
}
运行结果:
例如有这样一个han字符串字符串:用户'han'的名称zhong