Feedback

Alle E-Mail Adressen aus einem String bzw. Text filtern

Sprache: C#

Diese Funktion ließt aus einem String bzw. Text alle E-Mail Adressen aus und gibt diese in einer Liste zurück. Dies ist eine Übersetzung des Snippets von Tim. http://dotnet-snippets.de/dns/alle-e-mail-adressen-aus-einem-string-bzw-text-filtern-SID758.aspx
/// <summary>
/// Gets all Email addresses.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
private static List<string> GetAllEMailAddresses(string text)
{
    List<string> result = new List<string>();
    System.Text.RegularExpressions.MatchCollection regexMatchCollection = System.Text.RegularExpressions.Regex.Matches(text, @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*");
    for (int i = 0; i < regexMatchCollection.Count; i++)
        if (!result.Contains(regexMatchCollection[i].Value))
            result.Add(regexMatchCollection[i].Value);
    return result;
}
/// <summary>
/// Gets all Email addresses.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
private static List<string> GetAllEMailAddresses(string text)
{
    List<string> result = new List<string>();
    System.Text.RegularExpressions.MatchCollection regexMatchCollection = System.Text.RegularExpressions.Regex.Matches(text, @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*");
    for (int i = 0; i < regexMatchCollection.Count; i++)
        if (!result.Contains(regexMatchCollection[i].Value))
            result.Add(regexMatchCollection[i].Value);
    return result;
}

1 Kommentar