views:

305

answers:

5

Possible Duplicate:
What is the best regular expression for validating email addresses?

I am using this particular regular expression for checking emails.

"^[A-Za-z0-9](([a-zA-Z0-9,=\.!\-#|\$%\^&\*\+/\?_`\{\}~]+)*)@(?:[0-9a-zA-Z-]+\.)+[a-zA-Z]{2,9}$"

The issue I have is that, this allows periods before the "@" symbol. Is there any way this expression be modified so that this does not happen, yet all other conditions are maintained.

[email protected] should be invalid

Thanks in advance -Rollin

+4  A: 

The best answer I've seen so far. Honestly, if you gave some indication of which language or toolset you were using, I would point you to the library that does it for you rather than telling you how to hand-roll a regular expression for this.

Edit: Given the additional information that this is on .NET, I would use the MailAddress class and abandon the thought of using regular expressions altogether like so:

public bool IsAddressValid(string text)
{
    try
    {
        MailAddress address = new MailAddress(text);
        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}

If there are additional requirements over and above validating the address itself (like making sure it is from a particular set of domains or some such) then you can do that with much simpler tests after you have verified that the address is valid as I suggested in another post.

Lee
I am using asp.net(VB.net) regular expression validator. I need to hand roll an expression because of some specific requirements I have been asked to incorporate. Thats the reason I cannot use one of the many regular expressions that are available online.
AgentHunt
@unknown Lee said "library" not "regular expression". If you read the link Lee posted, you would see that he's recommending *against* using any regular expression at all, because doing so is inherently incorrect.
Imagist
A: 

If your regex engine has lookbehind assertions then you can just add a "(?<!\.)" before the "@".

Michael Speer
I am using the regular expression validator in asp.net. Would this work there?
AgentHunt
Did it work when you tried it?
Imagist
A: 

you could put [^\.] before the @ so that it will allow any character except the dot

of course this is probably not what you want, so you could just put a [] with any legal characters in it

just in case someone has a email name (i mean the part before the @) that is just 1 character, you might need to get creative with the |

golden_eagle
AgentHunt
A: 

If you're doing this in Perl - the following script is an example

my $string = '[email protected]';
if($string =~/(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,9})/)
{
   print "gotcha!";
}
else
{
   print "nope :(";
}

As you can see, the Perl regex character \w handles periods gracefully. If you change $string to "[email protected]" it will fail.

dls
ahhh - and in the time it took me to write this, there were about five good answers given :)
dls
+2  A: 

A strange game. The only winning move is not to play.

Seriously, the only winning way to validate email addresses with a regular expression is to not validate email addresses with a regular expression. The grammar that accepts email addresses is not regular. It's true that modern regular expressions can support some non-regular grammars, but even so, maintaining a regular expression for such a complex grammar is (as you can see) nearly impossible.

The only reasonable use of regular expressions with email addresses that I can think of is to do some minimal checking on the client side (does it contain an @ symbol?).

What you should do is:

  1. Send an email to the email address with a link for the user to click. If the user clicks the link, the email address is valid. Furthermore, it exists, and the user is probably the one who entered the email address into your form. Not only does this 100% validate the email address, it gives you even more guarantees.

  2. If you can't do 1, use a prepackaged email validator. Better, use both 1 and 2.

  3. If you can't do 1 or 2, write a real parser to validate email addresses.

Imagist