1

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)
    {
        if (!regex.isMatch(line))
        {
            matches = false;
            break;
        }
    }

    if (matches)
    {
        Console.WriteLine("This line matches all of the regexes: ");
        Console.WriteLine(line);
        break;
    }
}

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).

Xantix
  • 3,321
  • 1
  • 14
  • 28

1 Answers1

0

Well, as my understanding about Regex in .NET. There is probably no solution for you to AND two regex. However, I come up with :

Community
  • 1
  • 1
cat916
  • 1,363
  • 10
  • 18