PHP regular expression tutorial - Complex regular expression examples

Home - Tutorials - Basics

In this article I will show you how to use PHP regular expressions in your own PHP scripts.

Tutorial info:

Name:PHP regular expression tutorial
Total steps:2

Bookmark PHP regular expression tutorial

Step 2 - Complex regular expression examples

PHP regular expression tutorial

Now as you know the theory and basic syntax of PHP regular expressions it's time to create and analyze some more complex cases.

User name check with regular expression

First start with a user name check. In case of a registration form you may want to control available user names a bit. Let's suppose you don't want to allow any special character in the name except "_.-" and of course letters and numbers. Besides this you may want to control the length of the user name to be between 4 and 20.

First we need to define the available characters. This can be realised with the following code:


After that we need to limit the number of characters with the following code:


At least we need to put it together:


In case of Perl compatible regular expression surround it with '/'. At the end the PHP code looks like this:

  1. $pattern = '/^[a-zA-Z0-9_.-]{4,20}$/';
  2. $username = "";
  4. if (preg_match($pattern,$username)) echo "Match";
  5. else echo "Not match";


Check hexadecimal color codes with regular expression

A hexadecimal color code looks like this: #5A332C or you can use a short form like #C5F. In both case it starts with a # and follows with exactly 3 or 6 numbers or letters from a-f.

So the first it starts as:


the following character range is:


and the length can be 3 or 6. The complete pattern is the following:


Here we use an or statement first check the #123 form and then the #123456 form. At the end the PHP code looks like this:

  1. $pattern = '/^#(([a-fA-F0-9]{3}$)|([a-fA-F0-9]{6}$))/';
  2. $color = "#1AA";
  4. if (preg_match($pattern,$color)) echo "Match";
  5. else echo "Not match";


Email check with regular expression 

At least let's see how we can check an email address with regular expressions. First take a careful look at the following example emails:

What we can see is that the @ is a mandatory element in an email. Besides this there must be some character before and some after it. More precisely there must be a valid domain name after the @.  

So the first part must be a string with letters a numbers or some special characters like _-. In pattern we can write it as follows:


The domain name always have a let's say name and tld. The tld is the .com, .us. .info and the name can be any string with valid characters. It means that the domain pattern looks like this:


Now we only need to put together the 2 parts with the @ and get the complete pattern:


The PHP code looks like this:

  1. $pattern = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/';
  2. $email = "";
  4. if (preg_match($pattern,$email)) echo "Match";
  5. else echo "Not match";

Previous Step of PHP regular expression tutorial

Tags: php regular expression, regular expression, php

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.0992