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); ...
}
}
Your class that wraps your parsing functionality should contain a private reference (possibly static if it’s a static usage) to the regex, if you’re concerned about that kind of stuff.
EDIT:
To me, it’s not really about the performance, as since there’s the internal caching and all that jazz that Jimmy mentioned, I’d imagine the creation of a regex is probably not as expensive as the actual regex processing. It’s more about design principles: the factory method or parsing utility is conceptually operating using some internal filter (regex) to generate a list for you. If it’s the same one used over and over, that’s conceptually something you’re creating once and then keeping around and using over and over.