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; }
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.
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.
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.
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.
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()); } }
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()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With