Is there a generic way to combine regexes together?
While coding a solution of one of the Euler problems, my code ended up performing like this:
List<String> expressions; //There are 50 regex expr in this list
List<Regex> regexes = new List<Regex>();
foreach (String expr in expressions)
regexes.add(new Regex(expr, RegexOptions.Compiled));
foreach (String line in File.ReadAllLines(...))
bool matches = true;
foreach (Regex regex in regexes)
matches = false;
Console.WriteLine("This line matches all of the regexes: ");
The approach above is innefficient since it scans every line in the file 50 times.
I would like to create one Regex that matches a line only if all 50 regexes would match that string. That way each line is only scanned once (and hopefully unmatching lines will fail earlier due to the more restrictive regex).
(I don't care where they match, I only need to know if they match).
From some of my CS classes, I seem to remember learning how to do this by hand by generating DFAs for each regex and then intersecting them.
So, does C# have a built-in way for combining arbitrary regexes with AND?
If not, how can I achieve the same result by creating a new regex based on ANDing two regexes together? (Even better would be an extension method to Regex).
Well, as my understanding about Regex in .NET. There is probably no solution for you to AND two regex. However, I come up with :
Lookup<TKey, TElement> data structure . The keys in here would be your a collection of lists of regex while the elements are your collection of strings. I think it would be search more efficient than yours.
You come up with your own state machine. How regex converted to state machine