8

A new question has arisen in relation to an earlier question of mine. I have some code that is using a regex to find email addresses. It's working great except that it returns duplicate matches. I searched this site and found a question from a long time ago that was dealing with a similar problem, and the answer had something to do with mixing the regex logic with a string[] and the Distinct() method. Unfortunately my understanding of arrays is still limited.

My code is placing all the regex matches into a MatchCollection. Aside from that, how do I go about interacting with this MatchCollection to only add unique matches to the regex?

0

1 Answer 1

11

You can do it using Distinct in .NET 3.5 or newer if you call Cast on your match collection so that you can use the LINQ extension methods:

MatchCollection matchCollection = Regex.Matches(input, pattern);
List<string> matches = matchCollection
    .Cast<Match>()
    .Select(m => m.Value)
    .Distinct()
    .ToList();

This assumes that you have the following usings at the top of your file:

using System.Linq;
using System.Collections.Generic;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.