This is a code I read on a forum.
public String replaceVariables(String input, Map<String, String> context) {
if(context == null || input == null)
return input;
Matcher m = Pattern.compile( "\\$\\{([^}]*)\\}" ).matcher( input );
// Have to use a StringBuffer here because the matcher API doesn't accept a StringBuilder -kg
StringBuffer sb = new StringBuffer();
while(m.find()) {
String value = context.get(m.group(1));
if(value != null)
m.appendReplacement(sb, value);
}
m.appendTail(sb);
return sb.toString();
}
I am confuse about [^}]*
. Can I use another character instead of }
?