In the simplified example, there are two 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 regular expressions, 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.
There are three steps here:
MatchCollection
's to IEnumerable<Match>
'sMatch.Success
property is trueCode:
IEnumerable<Match> combined = matchNoCase.OfType<Match>().Concat(matchCase.OfType<Match>()).Where(m => m.Success);
Doing this creates a new enumerator which only executes each step as the next result is fetched, so you only end up enumerating through each collection once, total. For example, Concat() will only start executing the second enumerator after the first runs out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With