Is it valid to create a static Regex object to be used by all threads in an ASP.NET application?

Yes, Regex objects are thread-safe. From the docs:

The Regex class is immutable
(read-only) and is inherently thread
safe. Regex objects can be created on
any thread and shared between threads.

You might want to consider using RegexOptions.Compiled too – although don’t assume it’ll help performance; measure!

Leave a Comment