Regex and Speed05/08/2022
In the series of these posts, I take a rather regular task, and implement it multiple times in an iteratively manner, while also improving performance.
The task is the following: given a string input with placeholders in it, and a set of placeholders to actual value mappings. Replace the placeholders in the input string with the actual text values from the map and return the final string. For example, given input string Hello [%placeholder%]! and map [%placeholder%] => World should return Hello World!. The placeholder's [%placeholder%] part is referred as the key, and World part as the value in this post.
To further clarify the task, these constraints are also true:
- placeholder keys are delimited with
[%at the beginning and%]at the end - placeholders are non-recursive (a placeholder's value may contain another (or the same) placeholder's key, but this should not be processed recursively)
- input text contains no placeholder that are embedded by other placeholders (such as
hello [%outer[%inner%]example%]) - the key of placeholders contain letters and underscores
- the same fixed set of placeholders are used on multiple input strings
- placeholders are used at most once per input text