It seems the OP is trying to solve the problem of rendering a template. While it is fun to use regex, the problem of rendering templates is well addressed by template engines.
A good template language is Mustache and a good template Engine is stubble. Even if you do not harness all the power of template engine, the overall code is still concise:
using Stubble.Core.Builders;
...
var args = new Dictionary<string, string> {
{"text1", "name"},
{"text2", "Franco"}
};
var stubble = new StubbleBuilder().Build();
saveText(stubble.Render("Hi, my {{text1}} is {{text2}} and {{text3}} and {{text4}}")
The default behavior of stubble is to render unknown placeholders as empty. You can however add a default, e.g. a string with value "null":
var stubble = new StubbleBuilder().Configure(settings =>
{
settings.AddValueGetter(typeof(object), (key, value, ignoreCase) => "null");
}).Build();
Dictionary<string, object> args = new Dictionary<string, object> {
{"text1", "name"},
{"text2", "Franco"}
};
saveText(stubble.Render("Hi, my {{text1}} is {{text2}} and {{text3}} and {{text4}}")
{text1}with{text.1}in both the key and the input string. The result of the OP's code becomesHi, my {text.1} is Franco..@"\{(\w+)\}"with@"\{(.+?)\}"and it will work.