Regular Expression Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Regular Expression Problem

Post 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))
 
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Regular Expression Problem

Post by s.dot »

Why is the hyphen in the first part preceded by a backslash? I don't think that one should be there.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Regular Expression Problem

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Regular Expression Problem

Post 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. :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Regular Expression Problem

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Regular Expression Problem

Post 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
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply