tags:

views:

372

answers:

6

is there a way to extract all email address from a plain text,Using C# .

eg my email address is [email protected] and his email is [email protected] should return [email protected],[email protected]

I have tried 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;
        }
+6  A: 

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

David Morton
removing ^ and $ works but had to add some tweaks to extract the emails from it .I have posted the answer herewith
Thunder
+1  A: 

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

Scott Vercuski
The section "The Official Standard: RFC 2822" is particularly important if you actually want to understand what you're getting in to with trying to find valid email addresses.
Guildencrantz
+1  A: 

Some time back I wrote a blog post about it. Check out and see if it helps

extract/get email address from string using vb.net

This is for vb.net but you can easily convert it into c#. Let me know if you need help.

Shoban
A: 

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
+2  A: 

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
A: 

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