4

It's not hard to do a email validation on a user entered textbox.

But this time I would to check if a string/paragraph contains email addresses

For example. I would like to check if there is email in the following string

"How are you today, please email me at abc@def.com if you are interested"

Please help.

Tien
  • 61
  • 1
  • 5
  • The string will be less than 200 characters. Would using Regrex be easier in this case – Tien Dec 02 '13 at 17:04

3 Answers3

7

Here's how you can achieve this:

string para =
    " kjqdshfkjsdfh dskjhskqjdfhk qdhjkdhfj kjhfksjdhf jhjhjhjh@hhhh.com jjhdjfhsfjjd jhjhjhj jkhjhdfjhdjdf@.com ";
var splittedText = para.Split(new char[] {' '});
var mails = splittedText.Where(s => s.Contains("@"));
foreach (var mail in mails)
{
    //here are all your mails  
}

And then validate using the following method:

private bool IsEmailValid(string mail)
{
    try
    {                
        MailAddress eMailAddress = new MailAddress(mail);
        return true;
    }
    catch (FormatException)
    {
        return false;  
    }
}

Or just use something like:

public static bool ValidateEmail(string str)
{                       
    return Regex.IsMatch(str, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • 3
    "string contains email". The address may not be the whole string. – Gusdor Dec 02 '13 at 16:19
  • 2
    How does that do the job? The OP has a paragraph of text and your code only accepts the email part - it'll throw a `FormatException` with the OP's input. – Dan Puzey Dec 02 '13 at 16:20
  • @GrantWinney Great :) – BRAHIM Kamel Dec 02 '13 at 16:24
  • 1
    @GrantWinney The entire question is about separating the email address from the text. Not about validating a single address in a string. The question states that it is "It's not hard to do a email validation on a user entered textbox." so we must assume that OP knows how to do this, but don't know how to seperate the email. Hence, this is not an answer to the question. – MAV Dec 02 '13 at 16:27
  • my answer was updated – BRAHIM Kamel Dec 02 '13 at 16:30
  • 1
    why do you use that complicated Split? You could just use Split() instead. – Mare Infinitus Dec 02 '13 at 21:08
  • please post your code and let's other members decide :) – BRAHIM Kamel Dec 03 '13 at 08:54
3

First you split everything based on a whitespace delimiter (email addresses usually don't contain whitespaces):

var tokens = input.Split(" ");

Now you can validate the different tokens. The most easy one would be checking for an @ symbol and progressing from there. You could use an extensive regex, but you might as well just try and send an email to that address and see if it worked. It's a lot easier and doesn't require a 500-character regex.

Alternatively you can follow @Julie's advise and work with the MailAddress class.

As @Chris correctly remarks: an email address with a whitespace is valid if it is contained within quotes. Since it is a very uncommon edgecase, I would still hang on to this method and add a specific check to your code that validates the combination of quotes and whitespaces.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • 2
    Email addresses don't usually contain whitespace but they can. eg. `"Fred Bloggs"@example.com`. http://tools.ietf.org/html/rfc5322#section-3.4 is long and it is generally the case that no regex actually does it properly, they just get it 99.999% right. This is usually enough of course. :) – Chris Dec 02 '13 at 16:20
  • @Chris: valid remark, I'll add it to my post. It is a very uncommon edge case though, I'd still say that this method applies and should add an explicit check for this. – Jeroen Vannevel Dec 02 '13 at 16:22
  • 1
    Also make sure you strip any "," or "." att the end. Example: Hello email me att foo@hello.com. – Half_Baked Dec 02 '13 at 16:23
  • 2
    @JeroenVannevel: Yeah. Its why I think you should never bother trying too hard to validate email addresses. Check a few things like present of an @ that you can use to reject but the only real way to validate an email address is to send an email to it. – Chris Dec 02 '13 at 16:23
  • cherry picking text from a paragraph is exactly what regex is for. Why would you avoid it? – Gusdor Dec 02 '13 at 16:23
  • @Gusdor: because emailaddresses are potentially extremely complex. Just [take a look](http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses) at the valid/invalid examples. I know I wouldn't have been able to tell what was what. – Jeroen Vannevel Dec 02 '13 at 16:26
  • @JeroenVannevel so write a simple regex! Consider this statement: "My regex will be complicated if i try to catch every case, so i will fall back to more complicated code that _is functionally the same_" – Gusdor Dec 02 '13 at 16:28
  • 1
    The problem is that there is no simple regex for email addresses.. The only thing valid emailaddresses have in common is the `@` symbol. That's it. If you do more than that but not the **entire** spectrum of emailaddress validation, you will have false negatives. That's why you should just send an email to the given string that contains `@` and see if it returns an error. It worked? Yay: working email. It didn't work? No harm done. – Jeroen Vannevel Dec 02 '13 at 16:30
2

First, this is a glorious post on validating email addresses with regex.

Second, this regex will do a lot of the work you need:

[a-zA-z0-9\.]+@[a-zA-z0-9\.]+\.[A-Za-z]+

Regular expression visualization

Debuggex Demo

Result from your sample text:

Match 1:    abc@def.com     39      11
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • `user@[IPv6:2001:db8:1ff::a0b:dbd0]` false negative. `"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com` false negative. `!#$%&'*+-/=?^_\`{}|~@example.org` false negative. Etc etc. Email address validation shouldn't be done trough simple regex, because there is no simple regex. I agree that these are uncommon edge cases and it will work for a large share of the existing addresses, but since you can just send an email and see if it worked, this doesn't cut it for me. – Jeroen Vannevel Dec 02 '13 at 16:32
  • @JeroenVannevel the article i linked says exactly that. would `\s(.+@.+\..+)\s` make you feel better? – Gusdor Dec 03 '13 at 08:15