Is Regex instance thread safe for matches in C#

Regex objects are read-only, and therefore are thread safe. It’s their returns, the Match objects that could potentially cause problems. MSDN confirms this:

The Regex class itself is thread safe and immutable (read-only). That is, Regex objects can be created on any thread and shared between threads; matching methods can be called from any thread and never alter any global state.

However, result objects (Match and MatchCollection) returned by Regex should be used on a single thread ..

I’d be concerned about how your Match collection is being generated in a way that might be concurrent, which could cause the collection to act kinda weird. Some Match implementations use delayed evaluation, which could cause some crazy behavior in that foreach loop. I would probably collect all the Matches and then evaluate them later, both to be safe and to get consistent performance.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)