Using Regex as a template engine is an efficient way to replace strings in a template. Regex replace method supports executing a method upon match. We can populate a dictionary and make a lookup on all matches. This way we do the replacement in a single pass, rather than executing multiple .Replace(). Since dictionaries can be made case insensitive we have an efficient way of doing case insensitive replacement, something .Replace() does not support.
Example:
// Case sensitive match
//var data = new Dictionary<string, string>();
// Case insensitive match (using ordinal)
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// Add all words the template can replace
data.Add("WORD1", "Replaced word 1");
data.Add("word2", "Replaced word 2");
var template = "This is a template.\\\r\\
This is kept: \\\\\\\\[noreplace\\\\\\\\]\\\r\\
This is not found: [notfound]\\\r\\
Replace 1: [word1]\\\r\\
Replace 2: [WoRd2]";
// Replace any [word] with content from data-dictionary lookup, ignoring escaped \\\\[ and \\\\]
// Note that we set a limit of 50 characters on replacement key, just to avoid excessive CPU use on malicious data
var content = Regex.Replace(template, @"(?<!\\\\\\\\)\\\\[(?<word>[^\\\\]]{1,50})\\\\](?<!\\\\\\\\)", delegate (Match match)
{
var word = match.Groups["word"].Value;
string ret = null;
if (data.TryGetValue(word, out ret))
return ret;
return match.ToString();
});
// Replace \\\\[ with [ and \\\\] with ]
content = Regex.Replace(content, @"\\\\\\\\([\\\\[\\\\]])", @"$1");
// Print result
Console.WriteLine(content);
I have kept support for escaping [ and ] using \[ and \]. This does add a second pass to the template. Also you may want to add RegexOptions.Compiled (as last parameter on Replace-call, see next example).
Cleaning up the code a bit we see how this can look nice:
void Main()
{
// Our template
var template = "This is a template.\\\r\\
This is kept: \\\\\\\\[noreplace\\\\\\\\]\\\r\\
This is not found: [notfound]\\\r\\
Replace 1: [word1]\\\r\\
Replace 2: [WoRd2]";
// Fill some data
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
{ "WORD1", "Replacement 1" },
{ "word2", "Replacement 2" }
};
// Convert template
var content = TemplateReplace(template, data);
// Print result
Console.WriteLine(content);
}
public string TemplateReplace(string template, Dictionary<string, string> data)
{
// Replace any [word] with content from data-dictionary lookup, ignoring escaped \\\\[ and \\\\]
// Note that we set a limit of 50 characters on replacement key, just to avoid excessive CPU use on malicious data
var content = Regex.Replace(template, @"(?<!\\\\\\\\)\\\\[(?<word>[^\\\\]]{1,50})\\\\](?<!\\\\\\\\)", delegate (Match match)
{
var word = match.Groups["word"].Value;
string ret = null;
if (data.TryGetValue(word, out ret))
return ret;
return match.ToString();
}, RegexOptions.Compiled);
// Replace \\\\[ with [ and \\\\] with ]
content = Regex.Replace(content, @"\\\\\\\\([\\\\[\\\\]])", @"$1", RegexOptions.Compiled);
return content;
}
Taking it to the next level we could even populate the replacement data from a model:
public class SomeModel
{
public string Name { get; set;}
public int PostalCode { get; set;}
}
void Main()
{
// Our template
var template = "This is a template.\\\r\\
This is kept: \\\\\\\\[noreplace\\\\\\\\]\\\r\\
This is not found: [notfound]\\\r\\
Replace 1: [word1]\\\r\\
Replace 2: [WoRd2]\\\r\\
Replace 3: [postalcode] [Name]";
// Set up our replacement data object
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
// Fill some data manually
data.Add("WORD1", "Replaced word 1");
data.Add("word2", "Replaced word 2");
// Fill data from another object (model)
// We create a sample object
var someModel = new SomeModel() { Name = "Tedd", PostalCode = 1364};
// Populate data with properties
PopulateObjectIntoDictionary(data, someModel);
// Convert template
var content = TemplateReplace(template, data);
// Print result
Console.WriteLine(content);
}
public void PopulateObjectIntoDictionary(Dictionary<string, string> data, object @object)
{
// Read the object into the replacement data object using reflection
foreach (PropertyInfo pi in @object.GetType().GetProperties())
data[pi.Name] = pi.GetValue(@object, null).ToString();
}
public string TemplateReplace(string template, Dictionary<string, string> data)
{
// Replace any [word] with content from data-dictionary lookup, ignoring escaped \\\\[ and \\\\]
// Note that we set a limit of 50 characters on replacement key, just to avoid excessive CPU use on malicious data
var content = Regex.Replace(template, @"(?<!\\\\\\\\)\\\\[(?<word>[^\\\\]]{1,50})\\\\](?<!\\\\\\\\)", delegate (Match match)
{
var word = match.Groups["word"].Value;
string ret = null;
if (data.TryGetValue(word, out ret))
return ret;
return match.ToString();
}, RegexOptions.Compiled);
// Replace \\\\[ with [ and \\\\] with ]
content = Regex.Replace(content, @"\\\\\\\\([\\\\[\\\\]])", @"$1", RegexOptions.Compiled);
return content;
}
The output would be:
This is a template.
This is kept: [noreplace]
This is not found: [notfound]
Replace 1: Replaced word 1
Replace 2: Replaced word 2
Replace 3: 1364 Tedd
Written by Tedd · Oslo, Norway
Follow the journal via RSS