Say we have the following string source and want to replace a substring.
string source = "I am researching searchlights";
Regex.Replace(source, @"(?\w*)search(?\w*)", @"${wc1}pair${wc2}");
source.Replace("search", "pair");
Both of them will return:
I am repairing pairlights
If you are going to use Regex technique for a long text (longer than say 3-5 lines), it will work very slow and you don’t want to submit an entire document to a Regex function in a production application.
There are probably several ways to engineer this for performance (for instance, using a plain string search to find a match, and Regex to do the replacement on a smaller substring of the document),
If performance is an issue – never use regex at all. There are much more efficient techniques