I have a pretty similar situation.
Use the compilation option for regular expression:
Source = Regex.Replace(source, pattern, replace, RegexOptions.Compiled);
Depending on your situation, this can significantly affect speed.
Not a complete solution, especially for files larger than 3-4 MB.
If you need to decide which regular expression should be run (not my business), you should optimize the regular expression whenever possible, avoiding costly operations. For example, avoid unscrupulous operators, avoid looking forward and looking back.
Instead of using:
<a.*?>xxx
using
<a[^<>]*>xxx
The reason is that the roughness operator forces the regular expression engine to check each character in comparison with the rest of the expression, while [^ <>] only requires the comparison of the current character with <and> and stops as soon as the condition is agreed. In a large file, this can make the difference between half a second and freezing the application.
This does not completely solve the problem, but it should help.
Sylverdrag
source share