Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract all email address from a text using c#

Tags:

c#

.net

regex

email

Is there a way to extract all email addresses from a plain text using C# .

For example

my email address is [email protected] and his email is [email protected]

should return

[email protected], [email protected]

I have tried the following but it matches perfect emails only.

 public const string MatchEmailPattern =             @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"             + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."               + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"             + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";           public static bool IsEmail(string email)         {             if (email != null) return Regex.IsMatch(email, MatchEmailPattern);             else return false;         } 
like image 620
Thunder Avatar asked Feb 25 '10 12:02

Thunder


People also ask

How do I extract email addresses from text messages?

Email Extractor - Online Tool for Extracting Email Address Simply insert your source text content that includes the email addresses in provided text-box, click the "Extract Emails" button, and you'll get a clean list of unique email addresses.

How do I extract email addresses from URL?

Extract email addresses from a whole website Press ctrl+u to see the page's source code. Select all the source code, then copy. Go on extractemailaddress.com, then paste the code (ctrl+v) in the text box First step: Paste your text here. Copy and paste the result into a temporary file.

Is there a way for me to extract all the email addresses in my Gmail account?

Click the “More” tab above your contacts and select the “Export” option. In the next dialogue box, click on “Group” and select the group from which you want to download your email list. You have the option to choose your list from your contact list, most contacted, or any other group you created.

How do I extract email addresses from a string in Python?

To extract emails form text, we can take of regular expression. In the below example we take help of the regular expression package to define the pattern of an email ID and then use the findall() function to retrieve those text which match this pattern.


2 Answers

check this snippet

using System.IO; using System.Text.RegularExpressions; using System.Text;  class MailExtracter {      public static void ExtractEmails(string inFilePath, string outFilePath)     {         string data = File.ReadAllText(inFilePath); //read File          //instantiate with this pattern          Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",             RegexOptions.IgnoreCase);         //find items that matches with our pattern         MatchCollection emailMatches = emailRegex.Matches(data);          StringBuilder sb = new StringBuilder();          foreach (Match emailMatch in emailMatches)         {             sb.AppendLine(emailMatch.Value);         }         //store to file         File.WriteAllText(outFilePath, sb.ToString());     } } 
like image 71
Meysam Javadi Avatar answered Sep 22 '22 12:09

Meysam Javadi


Following works

public static void emas(string text) {     const string MatchEmailPattern =       @"(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"       + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."       + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"       + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})";       Regex rx = new Regex(        MatchEmailPattern,        RegexOptions.Compiled | RegexOptions.IgnoreCase);       // Find matches.       MatchCollection matches = rx.Matches(text);       // Report the number of matches found.      int noOfMatches = matches.Count;       // Report on each match.      foreach (Match match in matches)      {        Console.WriteLine(match.Value.ToString());      } } 
like image 35
Thunder Avatar answered Sep 21 '22 12:09

Thunder