<%@ page import="java.util.regex.Pattern" %>
<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="org.apache.commons.lang.StringUtils" %><%--
Created by IntelliJ IDEA.
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
//String callbackurl="http://gad.netease.com/mmad/click?s=o%2FYhx2YEuzG0A4tdBTuxfvAWBzo%3D&project_id=11612469&monitor_type=4&idfa={idfa}&imei={idfa}";
//
// String s = "dsadsadas({idfa})<peter>dsadasdas<lionel>\"www.163.com\"<kenny>{idfa}<>";
// Pattern p = Pattern.compile("(\\{[^>]*})");
// Matcher m = p.matcher(s);
// List<String> result=new ArrayList<String>();
// while(m.find()){
// result.add(m.group());
// }
// for(String s1:result){
// System.out.println(s1);
// }
Map<String,String> tokens = new HashMap<String,String>();
tokens.put("cat", "Garfield");
tokens.put("beverage", "coffee");
tokens.put("jim", "ccf");
//匹配类似velocity规则的字符串
String template = "{cat} really needs {jim} some {beverage}.";
//生成匹配模式的正则表达式
String patternString = "\\{(" + StringUtils.join(tokens.keySet(), "|") + ")\\}";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(template);
//两个方法:appendReplacement, appendTail
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
}
matcher.appendTail(sb);
//out: Garfield really needs some coffee.
System.out.println(sb.toString());
%>
转载于:https://my.oschina.net/u/564141/blog/669234