Handy PHP
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index

Refer A Friend using Revolution Money Exchange
A Few Handy PHP Regular Expressions (RegEx) PDF Print E-mail
Written by Handy PHP Administrator   
Thursday, 17 January 2008 17:38

    I frequently find that I am looking around the Internet for various Regular Expressions and sometimes I tend to have trouble finding just the right one.Then when I do, I use it and forget all about it! 

    While I know quite a bit about regex, I haven't mastered the techniques and figure if someone else has already found the regex for what I need, it is easier  and more time efficient to use that one.  Again, the problem for me is that I usually forget what script I used what regex in and have to go look it up again or search through a bunch of scripts on my computer that I've forgotten about.

     So I've decided to use this article as a regex repository that I'll add useful expressions and links to resources that will help you with your regex needs. 

Before I begin, I want to list a number of very good Regular Expression resources:


I'll add more as I find them...

 Now for the regular expressions I've used:

Email Address:

'/^[^\W][a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*\@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,6}$/' 

Matches: This e-mail address is being protected from spambots. You need JavaScript enabled to view it , This e-mail address is being protected from spambots. You need JavaScript enabled to view it , This e-mail address is being protected from spambots. You need JavaScript enabled to view it , This e-mail address is being protected from spambots. You need JavaScript enabled to view it

Domain Name 1: This only matched simple domain names

'/^([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}$/'

Matches: domain.com, domain.net
Non-matches: sub.domain.com, domain.uni.cc, domain.ne.jp

Domain Name 2: This only matches complex domain names

'/^([a-zA-Z0-9-]+)\.[a-zA-Z]{2,3}\.[a-zA-Z]{2}$/'

Matches: domain.uni.cc, domain.ne.jp
Non-matches: sub.domain.com, domain.com

Sub Domain: This will only match for a sub domain of the specified domain

'/^[a-zA-Z0-9-]+\.realdomainname\.com$/'

Matches: help.realdomainname.com, subdomain.realdomainname.com
Non-matches: sub.domain.com, help.realdomainname.net

Last Updated on Friday, 18 September 2009 06:13