Skip to content

Instantly share code, notes, and snippets.

@internetbird
Created March 9, 2015 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save internetbird/f3f01bf021dbfc2a6860 to your computer and use it in GitHub Desktop.
Save internetbird/f3f01bf021dbfc2a6860 to your computer and use it in GitHub Desktop.
Regex Template Parser
public class TemplateParser
{
public string ParseTemplate(string template, object data)
{
var tokensRegex = new Regex(@"\{{([^}]+)}}");
return tokensRegex.Replace(template, match =>
{
string tokenName = match.Groups[1].Value;
PropertyInfo property = data.GetType().GetProperty(tokenName);
if (property != null)
{
return property.GetValue(data, null).ToString();
}
return match.Value;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment