C# Code Snippet - Extract Emails

C# Code Snippet - Extract Emails

C# Code Snippet - Extract Emails

(C-Sharp) C# code snippet to extracts all the Emails from a string. ExtractEmails returns string array of Emails successful matches by iteratively applying a regular expression pattern to the input string.

Bookmark:

C# Code Snippet - Extract Emails

This .Net C# code snippet extracts all the Emails from a string. Data mining for Emails done by set of successful matches found by iteratively applying a regular expression pattern to the input string.

public string[] ExtractEmails(string str)
{
    string RegexPattern = @"\b[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}\b";

    // Find matches
    System.Text.RegularExpressions.MatchCollection matches
        = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    string[] MatchList = new string[matches.Count];

    // add each match
    int c = 0;
    foreach (System.Text.RegularExpressions.Match match in matches)
    {
        MatchList[c] = match.ToString();
        c++;
    }

    return MatchList;
}


C# Keywords Used:

  • Regex
  • Match
  • MatchCollection
  • RegexOptions.IgnoreCase
  • Regex.Matches

Code Snippet Information:

  • Applies To: .Net, C#, CLI, Data Mining, Email Extract, Regular Expression
  • Programming Language : C#

External Resources:

wael :: March 18-2010 :: 10:46 PM

Thank you for your code. it very good code

khalil :: November 11-2010 :: 09:53 AM

thanks , it was usefull to me

Marc :: December 05-2010 :: 08:18 PM

thanks! easy to use, reliable so far.

Joao :: April 26-2011 :: 02:20 PM

Good solution, Thanks;

Leave a comment