Code to extract Email addresses from text

public List<string> GetEmailsFromText(string text)
{
            Regex regex = new Regex(@”[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,6}”, RegexOptions.IgnoreCase);
            Match match = null;

            List<string> results = new List<string>();
            for (match = regex.Match(text); match.Success; match = match.NextMatch())
                if (!(results.Contains(match.Value)))
                    results.Add(match.Value);

            return results;
}

// Try using GetEmailsFromText(“email@yahoo.com, email2@gmail.com, email3@hotmail.com”);

Leave a Reply

Your email address will not be published. Required fields are marked *