Skip to content

Instantly share code, notes, and snippets.

@pykong
Created April 27, 2018 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pykong/d32df34f6d804d61162ac5a84ee96df1 to your computer and use it in GitHub Desktop.
Save pykong/d32df34f6d804d61162ac5a84ee96df1 to your computer and use it in GitHub Desktop.
internal static class MailExtractor
{
public static List<string> ExtractEmails(string data)
{
List<string> ExtractedEmailAddresses = new List<string>();
//instantiate with this pattern
string EmailPattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
Regex emailRegex = new Regex(EmailPattern, RegexOptions.IgnoreCase);
//find items that matches with our pattern
MatchCollection emailMatches = emailRegex.Matches(data);
foreach (Match emailMatch in emailMatches)
{
ExtractedEmailAddresses.Add(emailMatch.Value);
}
return ExtractedEmailAddresses.Distinct().ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment