This is a followup to this question
The first two answers are both correct and complete and at the end of the day, produce exactly the same result. However, one uses a Regex object and calls the aRegex.Replace(…) method (Joel’s answer) and the other uses the static Regex.Replace(…) method. (CMS’ answer).
Which method is preferred?
Under what circumstances would you change your mind?
Using the static instance will create a new
Regexobject each time so it is better to instantiate it yourself. Here is what I found using Reflector on System.dll:Plus if you instantiate your own instance you will be able to compile it as well and improve performance for multiple uses.
You can send
RegexOptions.Compiledto one of the staticReplaceoverloads but this is pointless as theRegexobject that will be instantiated with this flag cannot be used again.