I have a number of user defined Regexes that I’m using to test some strings to see If I should exclude them.
private readonly IEnumerable<Regex> exclusions = <some user defined source>;
static bool Exclude(string value)
{
return this.exclusions.Any(regex => regex.IsMatch(value);
}
I’m thinking, is there some way to compound the set of exclusions into a single combined exclusion Regex which would allow me to remove the iteration from the Exclude function, like this.
static bool Exclude(string value)
{
return this.exclusion.IsMatch(value);
}
Is this approach feasible, would it have a potential performance benefit or is it just pushing the burden of iteration down into the Regex processor?
EDIT
So, I could simply do,
var exclusion =
new Regex(string.Join("|", exclusions.Select(e => e.ToString()));
Is there any more sophisticated option?
EDIT 2
I’ve decided that since I have no control over the regular expressions, combining them blind is a naive approach, more likely to result in bugs than improved performance.
Logical OR: