Skip to content

Instantly share code, notes, and snippets.

@raykrueger
Created March 18, 2009 15:39
Show Gist options
  • Save raykrueger/81197 to your computer and use it in GitHub Desktop.
Save raykrueger/81197 to your computer and use it in GitHub Desktop.
public class TokenReplacement {
private static final Logger log =
LoggerFactory.getLogger(TokenReplacement.class);
private static final Pattern TOKEN_PATTERN = Pattern.compile("\\$\\{(.*?)\\}");
public static String replaceTokens(final String input, Properties props) {
Matcher matcher = TOKEN_PATTERN.matcher(input);
StringBuilder output = new StringBuilder();
while (matcher.find()) {
if (log.isDebugEnabled()) {
log.debug("Found the text \"{}\" starting at " +
"index {} and ending at index {}",
new Object[]{matcher.group(), matcher.start(), matcher.end()});
}
matcher.appendReplacement(output, props.getProperty(matcher.group(1)));
}
matcher.appendTail(output);
return output.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment