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.

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.


01 public string[] ExtractEmails(string str)

02 {

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

04

05     // Find matches

06     System.Text.RegularExpressions.MatchCollection matches

07         = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

08

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

10

11     // add each match

12     int c = 0;

13     foreach (System.Text.RegularExpressions.Match match in matches)

14     {

15         MatchList[c] = match.ToString();

16         c++;

17     }

18

19     return MatchList;
20 }

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

Spam protection by WP Captcha-Free