Here you can find the source of replaceTags(String str, Map
public static String replaceTags(String str, Map<String, String> tags)
//package com.java2s; //License from project: Apache License import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static final transient Pattern patternTag = Pattern.compile("<([a-zA-Z0-9_]*)>"); public static String replaceTags(String str, Map<String, String> tags) { StringBuffer ret = new StringBuffer(); Matcher matcher = patternTag.matcher(str); while (matcher.find()) { String tag = matcher.group(1); String repl = tags.get(tag); if (repl == null) { matcher.appendReplacement(ret, "<" + tag + ">"); } else { matcher.appendReplacement(ret, repl); }//from ww w .j a v a 2s . co m } matcher.appendTail(ret); return ret.toString(); } }