469,271 Members | 904 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Email address regular expression

I've been using this pattern to verify email addresses:

^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

But I've recently discovered that addresses with a dash in them won't
pass this test.

Seems to me they should pass.

What's wrong?

--
Floydian Slip(tm) - "Broadcasting from the dark side of the moon"
Random Precision Productions(tm)
67 Union St. #2D, Winooski, Vt. 05404-1948 USA
Sundays, 7-8 pm - Champ 101.3 FM, Colchester; 102.1 FM, Randolph, Vt.
cc*@floydianslip.com - AIM: RandomPrec - www.floydianslip.com
Jul 17 '05 #1
11 7439
Craig Bailey wrote:
I've been using this pattern to verify email addresses:

^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

But I've recently discovered that addresses with a dash in them won't
pass this test.

Seems to me they should pass.


[A-Z] = All characters from A to Z, maybe a \- might help ?

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #2
In article <Qp*********************@news-server.bigpond.net.au>,
"127.0.0.1" <newsgroup(at)cr*********@verisign-sux-ijlkl.com> wrote:
Craig Bailey wrote:
I've been using this pattern to verify email addresses:

^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

But I've recently discovered that addresses with a dash in them won't
pass this test.

Seems to me they should pass.


[A-Z] = All characters from A to Z, maybe a \- might help ?


Aaaahhhh. But a dash doesn't need to be escaped, do it? Are you
suggesting a add a dash to the first part of the expression? Like this?

^([0-9a-z-]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

Of course, that would allow someone to start their address with a dash,
which I'm not certain is allowable in real email addresses.

--
Floydian Slip(tm) - "Broadcasting from the dark side of the moon"
Random Precision Productions(tm)
67 Union St. #2D, Winooski, Vt. 05404-1948 USA
Sundays, 7-8 pm - Champ 101.3 FM, Colchester; 102.1 FM, Randolph, Vt.
cc*@floydianslip.com - AIM: RandomPrec - www.floydianslip.com
Jul 17 '05 #3
[ not posted to alt groups ]

Craig Bailey wrote:
I've been using this pattern to verify email addresses:

^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

But I've recently discovered that addresses with a dash in them won't
pass this test.

Seems to me they should pass.

What's wrong?


Doesn't [0-9a-z\.-_] mean: 0 to 9 and a to z and dot to underscore?

try [0-9a-z\._-]
HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #4
Hello,

On 10/06/2003 07:16 AM, Craig Bailey wrote:
I've been using this pattern to verify email addresses:

^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

But I've recently discovered that addresses with a dash in them won't
pass this test.

Seems to me they should pass.

What's wrong?


Character ranges that include - must start with that character. Take a
look here at this e-mail address validation class that comes with a
suitable regular expression besides the server based e-mail verification
methods.

http://www.phpclasses.org/emailvalidation
--

Regards,
Manuel Lemos

Free ready to use OOP components written in PHP
http://www.phpclasses.org/

Jul 17 '05 #5
Pedro wrote:
Doesn't [0-9a-z\.-_] mean: 0 to 9 and a to z and dot to underscore?
Yep. That's dot to underscore in "ASCII collating sequence". "Dot
to underscore" matches fifty characters in total, including all
uppercase letters, numbers, and a bunch of others -- but not the
hyphen!
try [0-9a-z\._-]


Or even [\w.-]. ;-)

--
Jock
Jul 17 '05 #6
Craig Bailey wrote:
Aaaahhhh. But a dash doesn't need to be escaped, do it?
Not necessarily. But in its current position, yes, because it's
indicating a range. The *hyphen* may only be left unescaped "where
it cannot be interpreted as indicating a range".
Of course, that would allow someone to start their address with a dash,
which I'm not certain is allowable in real email addresses.


I am certain it is.

See RFC 2822 for details, especially section 3.4.1.

--
Jock
Jul 17 '05 #7
On Mon, 06 Oct 2003 11:56:14 -0300, Manuel Lemos <ml****@acm.org> wrote:
Character ranges that include - must start with that character.


Or end with it.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #8
John Dunlop wrote:
Or even [\w.-]. ;-)


that will match (in some locales) "josécuña"

ps. hope that gets out ok, in HTML I'd write it as
jos&eacute;cu&ntilde;a

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #9
Pedro wrote:
John Dunlop wrote:
Or even [\w.-]. ;-)
that will match (in some locales) "josécuña"


Ah. Well spotted! I forgot what the intended use was, and
concentrated on the one part. Thanks.
ps. hope that gets out ok


It did.

--
Jock
Jul 17 '05 #10
Craig Bailey <cc*@floydianslip.com> wrote in message news:<cc***********************@news.verizon.net>. ..
I've been using this pattern to verify email addresses:

^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-_]+)\.([0-9a-z]+)

But I've recently discovered that addresses with a dash in them won't
pass this test.

Seems to me they should pass.

What's wrong?


In the book "Mastering Regular Expressions" there is an approximation
for internet email addresses, emails are to complex to be handed by
regular expressions correctly.

This regular expression is about 4800 characters long.

The best way is to not check any email addresses. Maybe assuming that
there must be a '@' is the only thing. Escpecially now where UTF can
be used in domain names, every [a-z] kind of pattern is simply wrong.
Jul 17 '05 #11
Lothar Scholz wrote:
In the book "Mastering Regular Expressions" there is an approximation
for internet email addresses, emails are to complex to be handed by
regular expressions correctly.


I agree...
usually what I check is to have smth before the @ and at least a word after
and that the mail address ends with a valid TLD...

--
WM
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

117 posts views Thread by Steevo | last post: by
35 posts views Thread by Mika M | last post: by
4 posts views Thread by Nick Bell | last post: by
1 post views Thread by Jim Dornbush | last post: by
9 posts views Thread by chadlupkes | last post: by
5 posts views Thread by Ganesh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.