There are two ways to "compile" a regular expression in .NET. Regular expressions are always “compiled” before they can be used to find matches. When you instantiate the Regex class without the RegexOptions.Compiled flag, your regular expression is still converted to the internal data structure used by the Regex class. The actual matching process is done in this data structure, not in the string representing your regular expression. It persists as long as your Regex instance lives on.
Explicitly creating an instance of the Regex class is preferable to calling static Regex methods if you use the same regular expression more than once. The reason is that static methods create an instance of Regex anyway, and then drop it. They maintain the cache of newly compiled regular expressions, but the cache is quite small, and searching the cache is much more expensive than simply referencing a pointer to an existing instance of Regex.
The above form of compilation exists in every programming language or library that uses regular expressions, although not everyone offers control over it.
The .NET framework provides a second way to compile regular expressions by creating a Regex object and defining the RegexOptions.Compiled flag. The absence or presence of this flag does not indicate whether the regular expression is compiled. It indicates whether the regular expression is compiled as described above, or in full, as described below.
What RegexOptions.Compiled really does is create a new assembly with your regular expression compiled before MSIL. Then this assembly is loaded, compiled into machine code and becomes a permanent part of your application (during its launch). This process takes up a lot of processor ticks, and memory usage is constant.
You should use RegexOptions.Compiled only if you process so much data with it that the user really needs to wait for your regular expression. If you can’t measure the speed difference using a stopwatch, don’t worry about RegexOptions.Compiled.
Jan goyvaerts
source share