package com.yundi.atp.platform.util;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class TemplateParseUtil {
private final static Pattern pattern = Pattern.compile("((?<=\{)([a-zA-Z\d]+)(?=\}))");
public static String regParse(String template, Map map) {
Matcher matcher = pattern.matcher(template);
while (matcher.find()) {
String key = matcher.group();
template = template.replaceAll("\$\{" + key + "\}", map.get(key) + "");
}
return template;
}
public static void main(String[] args) {
String template = "${testOne} is a good ${testTwo}!";
Map map = new HashMap(16);
map.put("testOne", "I");
map.put("testTwo", "Man");
String result = regParse(template, map);
System.out.println("result:" + result);
}
}