When you are using a Regex instance, that is used in a method where that method is called a couple thousand times to parse things in a certain way, should that method include the Regex instance, or should the Regex instance be part of the class as a static member?
I get the feeling that initializing the same Regex thousands of times might be an overhead. But I am mainly concerned as best practice.
Where should I declare and define the Regex?
EDIT: Pseudo code:
static Regex regex ...
IEnumerable<string> Parse (string str)
{
var matches = // use regex
foreach (var match in matches)
{
...
}
}
void Main()
{
foreach (var page in pages)
{
Parse (page); ...
}
}