Advanced Topics in Regular Expressions
Learn advanced topics of invariant culture, testing for correctness, performance, and security considerations with regular expressions.
Using invariant culture with regular expressions
When we perform operations using regular expressions, such as Replace
and Match
, the current thread’s culture determines the invariant culture.
The invariant culture is a CultureInfo
object that represents a culture that’s not influenced by the user’s regional settings.
The following code shows how to set the current thread’s culture to the invariant culture:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
The following code shows how to set the current thread’s UI culture to the invariant culture:
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
Note: Setting the current thread’s culture or UI culture to the invariant culture only affects the current thread. Other threads in the same process are not affected.
If we use regular expressions in a multithreaded application, we should set the current thread’s culture and UI culture to the invariant culture before we call any methods that use regular expressions. This ensures that the results of the ...