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

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:

Many e-mail addresses will contain additional dots:

I would change the first regexp to:

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:

But this would also match:

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://327951]
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
Find Nodes?
    Voting Booth?

    No recent polls found