Created
March 9, 2015 10:41
-
-
Save internetbird/f3f01bf021dbfc2a6860 to your computer and use it in GitHub Desktop.
Regex Template Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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