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

Viewed 3552

I need to know if it's safe to create a static Regex object like this:

public static Regex s_Regex_ExtractEmails = new Regex(@"\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b");

And call it statically from ASP.NET threads like this:

s_Regex_ExtractEmails.Matches("my email is mail@email.com")

Would this cause any problems?

I am doing this basically as an optimization so that the Regex object can be precompiled and reused.

3 Answers
Related