public static void main(String[] args) { Pattern regex = Pattern.compile("\\$\\{([^}]*)\\}"); String a ="hello"; String b = "wlow"; Map<String,String> paramData = Maps.newHashMap(); paramData.put("1",a); paramData.put("2",b); String source ="您好,${1}分配了一個${2}的 上看看({3})給您,請您進行處理。"; Matcher matcher = regex.matcher(source); StringBuffer stringBuffer = new StringBuffer(); while(matcher.find()) { String key = matcher.group(1); String value = paramData.get(key); if(null == value){ value =""; } matcher.appendReplacement(stringBuffer, value); } matcher.appendTail(stringBuffer); System.out.println(stringBuffer.toString()); }
短信模板替换
最新推荐文章于 2022-10-10 16:09:41 发布