Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

Categories


Regex.Replace

baranbaran

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