Efficiently Combine MatchCollections in .Net Regex
Posted
by Laramie
on Stack Overflow
See other posts from Stack Overflow
or by Laramie
Published on 2010-05-26T23:07:37Z
Indexed on
2010/05/26
23:11 UTC
Read the original article
Hit count: 501
In the simplified example, there are 2 Regular Expressions, one case sensitive, the other not. The idea would be to efficiently create an IEnumerable collection (see "combined" below) combining the results.
string test = "abcABC";
string regex = "(?<grpa>a)|(?<grpb>b)|(?<grpc>c)]";
Regex regNoCase = new Regex(regex, RegexOptions.IgnoreCase);
Regex regCase = new Regex(regex);
MatchCollection matchNoCase = regNoCase.Matches(test);
MatchCollection matchCase = regCase.Matches(test);
//Combine matchNoCase and matchCase into an IEnumerable
IEnumerable<Match> combined= null;
foreach (Match match in combined)
{
//Use the Index and (successful) Groups properties
//of the match in another operation
}
In practice, the MatchCollections might contain thousands of results and be run frequently using long dynamically created REGEXes, so I'd like to shy away from copying the results to arrays, etc. I am still learning LINQ and am fuzzy on how to go about combining these or what the performance hits to an already sluggish process will be.
© Stack Overflow or respective owner