51

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

For example

my email address is mrrame@gmail.com and his email is mrgar@yahoo.com

should return

mrrame@gmail.com, mrgar@yahoo.com

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;
        }
Marc
  • 12,706
  • 7
  • 61
  • 97
Thunder
  • 10,366
  • 25
  • 84
  • 114
  • 6
    Remove the ^ from the start of the regex and the $ from the end. Is there a purpose other than spamming for this activity? – Lazarus Feb 25 '10 at 12:17
  • 1
    @Lazarus its not for spamming but for web crawling the address so that a contact database can be made ! removing ^ and $ works but had to add some tweaks to extract the emails from it .I have posted the answer herewith . – Thunder Feb 26 '10 at 04:24
  • try this http://stackoverflow.com/a/26274085/1604425 much better than complex regx – dhiraj Aug 31 '16 at 08:20

5 Answers5

83

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());
    }
}
Meysam Javadi
  • 1,374
  • 1
  • 10
  • 21
27

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());
     }
}
Thunder
  • 10,366
  • 25
  • 84
  • 114
  • 3
    How about this address: test@2s-company.com Your pattern doesn't extract these kind of addresses. – Salaros Nov 22 '13 at 10:25
  • ***to extract all email address from a plain text***, not log to console – PreguntonCojoneroCabrón Apr 27 '17 at 16:47
  • Please, have a look at these sites: TLD list: https://www.iana.org/domains/root/db ; valid/invalid addresses: https://en.wikipedia.org/wiki/Email_address#Examples ; regex for RFC822 email address: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html – Toto Sep 03 '18 at 09:26
  • 1
    @thunder, you better update your code to handle and such emails test@2s-company.com, otherwise a lot of people will get in trouble after some time. – Nic Dec 11 '18 at 07:53
  • @nic plz suggest – Thunder Dec 11 '18 at 15:27
6

Just remove the "^" from the beginning and the "$" from the end of your filter string.

David Morton
  • 16,338
  • 3
  • 63
  • 73
  • removing ^ and $ works but had to add some tweaks to extract the emails from it .I have posted the answer herewith – Thunder Feb 26 '10 at 04:25
3

give this a try http://www.regular-expressions.info/email.html

0

If you don't want it to match perfect email addresses, don't use a regular expression that matches perfect email addresses.

The regular expression you are using will match on the start of the line (^) and the end of the line ($), so if you remove those it will not filter with them.

Oded
  • 489,969
  • 99
  • 883
  • 1,009