Page 1 of 1
Regular Expression Problem
Posted: Sun Jul 06, 2008 8:36 pm
by dajawu
Hey guys,
I am using a Regular Expression to weed out bad email addresses. Now I know it isn't foolproof but for some reason it is blocking any hyphen in the first part of them email but not the domain part. For example
test-this@test.com will not work but
test@this-test.com will work. Here is my expression:
Code: Select all
elseif(!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',$email))
Re: Regular Expression Problem
Posted: Sun Jul 06, 2008 9:37 pm
by s.dot
Why is the hyphen in the first part preceded by a backslash? I don't think that one should be there.
Re: Regular Expression Problem
Posted: Sun Jul 06, 2008 9:42 pm
by dajawu
The Hyphen is a special character, so in order to match a hyphen you need to use a backslash. A backslash is needed to make any special character a normal character.
Re: Regular Expression Problem
Posted: Sun Jul 06, 2008 10:30 pm
by s.dot
I don't believe the hyphen is a special character. If you remove the backslash in front of it, the regex should work.

Re: Regular Expression Problem
Posted: Sun Jul 06, 2008 10:37 pm
by dajawu
Thanks for helping out but it is a special character. I did although try to take it out and that caused a warning and still didn't work. The weird part about it is you can use a hyphen in the domain part of the email address and I used \- to make it work. But the same exact code in the email part doesn't work!
Re: Regular Expression Problem
Posted: Sun Jul 06, 2008 10:45 pm
by s.dot
Sorry I couldn't help. Here's the regex I use to weed out bad emails..
Code: Select all
if(!preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9- ]+)*(\.[a-z]{2,3})$#i", $emailAddress))
{
//bad email here
}