码迷,mamicode.com
首页 > 其他好文 > 详细

${var}变量替换

时间:2016-03-07 13:44:53      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

${age},${name} 给定一个上下文 Map<String,String> context 使用上下文替换变量
private
static final Pattern VAR_PATTERN = Pattern.compile("\\$\\{[^\\}\\$\u0020]+\\}"); private static final int MAX_SUBST = 20; private static String substituteVars(String expr,Map<String,String> paramMap) { if (expr == null) { return null; } Matcher match = VAR_PATTERN.matcher(""); String eval = expr; for(int s=0; s<MAX_SUBST; s++) { match.reset(eval); if (!match.find()) { return eval; } String var = match.group(); var = var.substring(2, var.length()-1); // remove ${ .. } String val = null; try { val = paramMap.get(var); } catch(SecurityException se) { } /*if (val == null) { val = "aa"; }*/ if (val == null) { return eval; // return literal ${var}: var is unbound } // substitute eval = eval.substring(0, match.start())+val+eval.substring(match.end()); } throw new IllegalStateException("Variable substitution depth too large: " + MAX_SUBST + " " + expr); }

 

${var}变量替换

标签:

原文地址:http://www.cnblogs.com/zhengqun/p/5250098.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!