help with email regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

help with email regex

Post by s.dot »

Code: Select all

//invalid email address?
if(!preg_match("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*".
 "\.(([0-9]{1,3})|([a-zA-Z]{2,6})|(aero|coop|info|museum|name))$",$_POST['email']))
{
	header('Location: mailing-list-join.php?err=2');
	exit;
}
My email address I enter always fails -- valid or not. I've tried several regexes I've found googling, but they always fail. Is my PHP wrong? Or is it the regex?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

this is the one I have been using... I am a regex retard, so i really don't know how well it works, but I know it works.

Code: Select all

/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,}$/i
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

D'oh! I was concentrating on finding a good regex and forgot to put delimiters on my regex. This are the kinds of things that make me look stupid in public. :lol:

I'll give that one a try too, ninja. See which one i like better. Thanks.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The Ninja Space Goat wrote:this is the one I have been using... I am a regex retard, so i really don't know how well it works, but I know it works.

Code: Select all

/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,}$/i
This will allow:
someuser@yahoo.bogusdomain

[edit] Apparently PHPBB does, too.

[edit number 2] If you change {2,} to {2,6} I think it will be the most accurate you can get. I don't think there are any 7 letter TLDs.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why not use the RFC compliant regex that has been posted many times?

http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

feyd wrote:Why not use the RFC compliant regex that has been posted many times?

http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup
Aren't there valid emails that are not RFC compliant?

IE: emails that start with a hyphen.

I've seen some emails like -.superman.-@hotmail.com

[edit] I'm entirely guessing that that email is non-rfc compliant.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Test it. :)
User avatar
Draken Stark
Forum Newbie
Posts: 2
Joined: Mon Dec 18, 2006 12:29 pm

Email Regex

Post by Draken Stark »

Well here's a regex that my class and I made for a Perl lab that we did. Works with the large majority that's out there. We have yet to find anything wrong with it other than the top level domain area. (e.g. for instance you could have an email like this: al_fred@woopiedoo.superman . If it's not obvious the (dot)superman shouldn't exist unless some higher up in the government wanted it to or whatever... lol)

Edit: the previous regex code I had here had allowed some obviously invalid emails (example: whatever@second@at_symbol.com )

Here's the code that we had just recently optimized for a PHP Helpdesk project.
^([A-Za-z0-9_\+\-])+(\.[A-Za-z0-9_\+\-]+)*@([A-Za-z0-9_\+\-]+(\.[A-Za-z0-9_\+\-]+)*\.([A-Za-z0-9]){2,})$
User avatar
Draken Stark
Forum Newbie
Posts: 2
Joined: Mon Dec 18, 2006 12:29 pm

Top level domain list

Post by Draken Stark »

For those of you who wish to only be able to have valid top level domains check out this site:
http://data.iana.org/TLD/tlds-alpha-by-domain.txt

Feel free to be my guest to add them all >.< lol there's so many.
Post Reply