Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: regex to validate e-mail addresses and phone numbers

by Rhys (Pilgrim)
on Feb 10, 2004 at 16:03 UTC ( #327951=note: print w/replies, xml ) Need Help??


in reply to regex to validate e-mail addresses and phone numbers

If you insist upon writing your own regex, you're going to want to pay more attention to character classes, and you want to remember that not all e-mail addresses are in the format:

user@domain.com

Many e-mail addresses will contain additional dots:

user@mail.server.domain.info

I would change the first regexp to:

/^\w[\w\.\-]*\w\@\w[\w\.\-]*\w(\.\w{2,4})$/
I left the parenthetical part in place, since you're apparently trying to get the top-level domain (.edu, .com, etc.) into $1, but I took off the + at the end, since it's definitely in your way. I'm not even sure what it would do in this context. I also left intact the requirement that the user and host part should begin and end with a \w character, but may contain any number of dots or dashes. The way this reads, the minimum matching string would look like:

me@me.com

But this would also match:

my.big-name.sucks-big-time@mail.server-farm.long-domain.coop

Read up on character classes. They are your friends. Anyway, the biggest obvious remaining problem (in my opinion) with this regexp is it will still allow multiple consecutive dots or dashes. This may not be a problem in the user field, but consecutive dots are not allowed in the host field. It might be simpler to write a whole 'nother regexp to look for consecutive dots or dashes and reject based on that.

Replies are listed 'Best First'.
Re: Re: regex to validate e-mail addresses and phone numbers
by seanbo (Chaplain) on Feb 10, 2004 at 21:21 UTC
    FWIW, I use: /^\w+\@([\da-zA-Z\-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/ The main idea is that an "_" (underscore) matches on \w, but is not valid as part of a domain name. It is, however, acceptable as part of the username. I use {2,6} since the there are some tld's longer than 4 chars (.museum for example).


    perl -e 'print reverse qw/o b n a e s/;'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://327951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2020-11-30 09:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?