asp.net.ph

Skip Navigation LinksGetting Started > ASP.NET Base Types > Regular Expressions > Details of Regular Expression Behavior > Compilation and Reuse

Compilation and Reuse

ASP.NET Base Types   Regular Expressions   Details of Regular Expression Behavior


By default, the regex engine compiles a regular expression to a sequence of internal instructions ( these are high-level codes that are different from Microsoft intermediate language ( MSIL ) ). When the engine executes a regular expression, it interprets the internal codes.

If a Regex object is constructed with the RegexOptions.Compiled option, it compiles the regular expression to explicit MSIL code instead of high-level regular expression internal instructions. This allows the.NET Framework’s just-in-time ( JIT ) compiler to convert the expression to native machine code for higher performance.

However, generated MSIL cannot be unloaded. The only way to unload code is to unload an entire application domain ( that is, to unload all of your application’s code. ). Effectively, once a regular expression is compiled with the RegexOptions.Compiledoption, the .NET Framework never releases the resources used by the compiled expression, even if the Regex object itself is released and garbage-collected.

You must be careful to limit the number of different regular expressions you compile with the RegexOptions.Compiled option to avoid consuming too many resources. If an application must use a large or unbounded number of regular expressions, each expression should be interpreted, not compiled. However, if a small number of regular expressions are used repeatedly, they should be compiled with RegexOptions.Compiled for higher performance. An alternative is to use precompiled regular expressions. You can compile all of your expressions into a reusable dll. This avoids the need to compile at runtime while still benefiting from the speed of compiled regular expressions.

To improve performance,the regex engine caches all regular expressions in memory. This avoids the need to reparse an expression into high-level byte code each time it is used.

See Also

Regular Expressions Language Elements



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note