![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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])?$Last edited by Jimbo; Jun 29th, 2006 at 2:01 AM. |
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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!
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jun 29th, 2006 at 6:23 AM. |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
|
|
|
|
|
|
#4 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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 |
|
|
|
|
|
|
#5 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
I looked things over yet again, and here's what I came up with, broken down:
<letter> = '[A-Za-z]';
<letdig> = '[A-Za-z0-9]'
<locChar> = '[!#$%&\'*+/=?^_`{|}~A-Za-z0-9-]' # valid chars for the local part, minus the dot
<domChar> = '[A-Za-z0-9-]' # adds the hyphen to letdig
<local> = '(<locChar>+\.)*<locChar>+'
<domain> = '(<letter>(<domChar>*<letdig>)*\.)*<letter>(<domChar>*<letdig>)*'
<email> = '^<local>@<domain>$''^([!#$%&\'*+/=?^_`{|}~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])$' |
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 748
Rep Power: 3
![]() |
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])*$' |
|
|
|
|
|
#7 |
|
Expert Programmer
|
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.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|