Regex { public static final String resolveDelimitedTemplate(final String suffix, final String postfix, final String template, final Map<String, String> map) { if (suffix==null || postfix==null || template == null || map == null) { return null; } String buffer = new String(template); synchronized (map) { final Set<Entry<String, String>> set = map.entrySet(); final Iterator<Entry<String, String>> it = set.iterator(); while (it.hasNext()) { final Entry<String, String> entry = it.next(); final StringBuilder key = new StringBuilder(suffix).append(entry.getKey()) .append(postfix); final String temp; if ((temp = replace(buffer, key.toString(), entry.getValue(),true)) != null) { buffer=temp; it.remove(); } } } if (suffix.length()>0||postfix.length()>0){ buffer = buffer.replaceAll(suffix+"[A-z]+"+postfix, ""); } return buffer; }  static final String replace(final String template, final String regex,
            final String value, boolean all); static final String resolveTemplate(final String template, final Map<String, String> map); static final String resolveDelimitedTemplate(final String suffix, final String postfix,
            final String template, final Map<String, String> map);  }
@Test public void test() { String template="${ROOT}/${PATH}/${TO}/${FILE}"; final Map<String,String> map=new HashMap<String, String>(); map.put("ROOT", "/opt"); map.put("PATH", "program"); map.put("AT", "url"); map.put("FILE", "file.txt"); String res=Regex.resolveDelimitedTemplate("\\$\\{","\\}",template, map); assertTrue(!map.containsKey("PATH")); assertTrue(map.containsKey("AT")); System.out.println(res); assertTrue(res.equals("/opt/program }