![]() |
Email validating regex
I know this was discussed in another thread, but that one's been dead and I thought it seemed peaceful that way. Anyways, I was writing a regex for email addresses and came across a little question:
On this page it has the following language for domain names: Quote:
And for those who care, my regex (which is not perfect yet) is: :
^[!#$%\'*+/=?^_`{|}~A-Za-z0-9-]([!#$%\'*+/=?^_`{|}~\.A-Za-z0-9-]*[!#$%\'*+/=?^_`{|}~A-Za-z0-9-])@([A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])*\.)*[A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])?$ |
Hi Jimbo,
Have you read the rfc? The grammar described in RFC 822 is extremely complex. Many are quick to understimate email. Implementing validation with regular expressions somewhat pushes the limits of what it is sensible to do with regular expressions. Perl does it better than some : EDIT: ok, I am having trouble formatting it .. I have one I copied from "Mastering Regular Expressions". Let this much be said: it is over 4700 Bytes long! In any case, good luck mon ami! |
|
I hadn't read it the RFC, just glanced through it. I'd seen Ooble's link in the other thread and gotten a kick out of it. I was just trying to match the domain language (the one above) and the language for the local segment:
Quote:
And just looking at it after a nice night's sleep, I think it should be ([atext]*.)*[atext]* so I'll probably work things out again... maybe on paper this time... :o |
I looked things over yet again, and here's what I came up with, broken down:
:
<letter> = '[A-Za-z]';:
'^([!#$%&\'*+/=?^_`{|}~A-Za-z0-9-]+\.)*[!#$%&\'*+/=?^_`{|}~A-Za-z0-9-]+@([A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])\.)*[A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])$' |
regex had a bug for single-char domains... but should be good now :o
:
'^([!#$%&\'*+/=?^_`{|}~A-Za-z0-9-]+\.)*[!#$%&\'*+/=?^_`{|}~A-Za-z0-9-]+@([A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])*\.)*[A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])*$' |
You guys should check out a call website http://www.regexplib.com it is an online resource where people litterally post regexp strings for review for certain tasks, and they are even rated by the community.
Plus the website has a cool cheat sheet you can print on the syntax, and they have a flexible online app which will allow you to test regular expressions, very handy if you are using Windows I find since it doesn't have a good utility to do this for you. |
| All times are GMT -5. The time now is 1:34 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC