Using Regular Expressions in PHP

This is an article discussion thread for discussing the SitePoint article, “Using Regular Expressions in PHP

This regular expression has a copule of problems. Firstly, I get a REG_ERANGE error and to fix this the ‘-’ characters within the have to be escaped with a \. Secondly, three-part domain names fail, such as info@john.doe.name
The following works better for me:
[1]+@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,5})


  1. a-z0-9._\- ↩︎

worked fine for me, this is another alright insight to regex, ive been reading various different articles, this is another one that helped :slight_smile:

Why do all the incorrect email matching expressions come up when you search for them on google. This is yet another example of how the author didn’t completely do their homework to find out the complete matching expression for validating email. Sigh

A good overview of some of the possibilities with regex but as noted by others, many valid e-mail addresses will not be accepted by this regex string.
It doesn’t account for allowing an apostrophe in a user’s name (which is valid according to the relevant RFCs) and it seems to not allow domain extensions such as .co.uk, .com.au or .info.

and it seems to not allow domain extensions such as .co.uk, .com.au or .info.

Have you checked the code for that? Do you think the author didn’t check it? Of course it allows these domain extensions. I did the test and it worked. Just do it again and you will see… >:-(

This was the only place I could find that was able to explain the eregi function to me (php.net did, but didn’t teach syntax). I don’t believe the point of the tutorial was to give you a email validity checker, but that was rather an example of how you could use the code.

Very Helpful…thanks for taking the time to write this

I need to insert line breaks in the eregi expression, but I can’t figure out what should I use there. The
doesn’t work…

I don’t understand if the plus sign “+” requires one or more of the previous characters does’nt that mean if a user’s email address does not have one of the characters, say a “_” or “-” then it will not validate?

Nice intro, well written but not exactly definitive. some links to further reading would be nice.

JohnKelly, the characters included inside the are the ALLOWED characters that an email address may contain. It doesn’t mean that it must include all these.

The + character indicates that the username block of the address (the part before the @ sign) must have AT LEAST one character from the ones included inside the .

will this [1]+
do for the user name checking…?


  1. a-zA-Z0-9._- ↩︎

@joby

It probably won’t, because it will just check if something is in the string, even when " characters (for instance) cannot be allowed.

I suggest something like
[1]{4,}$

as this means: at least 4 characters of a-z A-Z and 0-9 and _ - and .

note: please correct me if I got this wrong =P I’m no regex expert!


  1. a-zA-Z0-9_-. ↩︎

WOW, this is WEIRD!!

Seriously, when you look at your COMMENT BENEATH the article it ONLY states the following:

Comment

will this ^+
do for the user name checking…?

Posted by: joby Oct 8th, 2005 @ 9:20 AM EDT

Then when I posted a reply I saw you actually posted the same I did :stuck_out_tongue: just didn’t include the $ at the end…

I’d still say my regex is a **** better than yours (otherwise folks might sign on with usernames like: ab, or __)

By the way, EVEN better is:
[1]{1}[a-zA-Z0-9_-.]{3,}$

as it makes sure the FIRST character of a username is a letter or digit… but whatever, it’s all the same :stuck_out_tongue:

note: again, correct me if I’m wrong… no expert!


  1. a-zA-Z0-9 ↩︎

Actually Quaint’s regex would work better if it was:

^[-a-zA-Z0-9_.]{4,}$

If you’re gonna use dashes (-) inside braces it must be put in the beggining or in the end, because it works like “From * to *”, meaning that it would try to much every character between the ASCII representation of underscore (_) and the ASCII representation of the dot (.)

Good explanation! Thank you!

Hi this is great i can now create my own regular expresion. I would only reuse already made expression cos i did not understand how they come up with them.
Thanx again

Good article, thanks.

Good article to start with Regular Expressions in PHP