Email address regular expression

This is a discussion on Email address regular expression within the PHP Language forums, part of the PHP Programming Forums category; I've been using this pattern to verify email addresses: ^([0-9a-z]+)([0-9a-z\.-_]+)@([0-9a-z\.-...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-06-2003
Craig Bailey
 
Posts: n/a
Default 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.
ccb@floydianslip.com - AIM: RandomPrec - www.floydianslip.com
Reply With Quote
  #2 (permalink)  
Old 10-06-2003
127.0.0.1
 
Posts: n/a
Default Re: Email address regular expression

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)craznar.com@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Reply With Quote
  #3 (permalink)  
Old 10-06-2003
Craig Bailey
 
Posts: n/a
Default Re: Email address regular expression

In article <Qpbgb.138702$bo1.108744@news-server.bigpond.net.au>,
"127.0.0.1" <newsgroup(at)craznar.com@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.
ccb@floydianslip.com - AIM: RandomPrec - www.floydianslip.com
Reply With Quote
  #4 (permalink)  
Old 10-06-2003
Pedro
 
Posts: n/a
Default Re: Email address regular expression

[ 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.
Reply With Quote
  #5 (permalink)  
Old 10-06-2003
Manuel Lemos
 
Posts: n/a
Default Re: Email address regular expression

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/

Reply With Quote
  #6 (permalink)  
Old 10-06-2003
John Dunlop
 
Posts: n/a
Default Re: Email address regular expression

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
Reply With Quote
  #7 (permalink)  
Old 10-06-2003
Andy Hassall
 
Posts: n/a
Default Re: Email address regular expression

On Mon, 06 Oct 2003 11:56:14 -0300, Manuel Lemos <mlemos@acm.org> wrote:

>Character ranges that include - must start with that character.


Or end with it.

--
Andy Hassall (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
  #8 (permalink)  
Old 10-06-2003
John Dunlop
 
Posts: n/a
Default Re: Email address regular expression

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
Reply With Quote
  #9 (permalink)  
Old 10-06-2003
Pedro
 
Posts: n/a
Default Re: Email address regular expression

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.
Reply With Quote
  #10 (permalink)  
Old 10-06-2003
John Dunlop
 
Posts: n/a
Default Re: Email address regular expression

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:36 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0