Retrieve or Extract all Emails from String Using C#.Net

How to extract or retrieve or get all valid email id from a string using c#,net by regularexpression or Regex. It works in web & winddows application.

In today's article I will show you how you can get or retrieve all the email id present in a given string using cc#.net. Here I have used regular expression to validate the email id and extract from passed string. Here for show the demo I have used a windows application to represent using C#.Net, you can also use in the console application also.

So, for this article first we will create a new windows application and add the two textbox control and a button control on the from. 

Retrieve or Extract all Emails from String Using C#.Net

After this we will create a function named as GetEmailCollectionFromString. This method will require a parameter of string type. The return type of the string will be MatchCollection. Now let's check the method code. 
 /// <summary>
 /// To get the regex match collection of email
 /// </summary>
 /// <param name="emailString">string</param>
 /// <returns></returns>
 private MatchCollection GetEmailCollectionFromString(string emailString)
 {
     Regex regex = new Regex("[A-Z0-9a-z._%+-]+@[A-Za-z0-9-]+\\.[A-Za-z]{2,64}");
     return regex.Matches(emailString);
 }
Credit for above Regex: stackoverflow.com
In above code I have created object for Regex class and passed the regular expression for email id. after that I have used Matches function to get the valid email ids. After this creating the function we will create a button click event to add the working code. Please check the below code.
  StringBuilder sb = new StringBuilder();
  var emailData = GetEmailCollectionFromString(txtEmailString.Text);
  foreach (var item in emailData)
  {
      sb.Append(item + ", ");
  }
  txtFinalString.Text = sb.ToString();
In above code I have first created the object for StringBuilder class. After that taken a variable and used the function GetEmailCollectionFromString. In this function I have passed the string value from which we want to extract the email id.

After getting value I have used foreach loop to read the extracted email ids and append it into the StringBuilder object.  Finally, I have assigned the extracted output to the output textbox. Now lets run the code to check the output.

We will add a string which is having some email id in it and an invalid email id. Please check below string having email id.

This is a sample one email id as email1@gmail.om, second email is email2@yahoo.in, third email id is email3@google.com. So we are having 3 correct email and one wrong which is email4@gmail

Place the string in input textbox.
Retrieve or Extract all Emails from String Using C#.Net

Now click on "Extract Email Id" button by placing a break point. As the code execution complete we will see three email ids.

Code to extract all email id from string in c#.net

In above code you can see that we are getting the correct email id into the list without invalid email id. Now press F5 to check the final output.

Extracted email id from string using c#.net

Post a Comment