当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to Combine Arbitrary Regexes with AND

问题描述:

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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: