PHP Regular Expressions (REGEX) tutorial for beginners

PHP regular expressions seems to be a quite complicated area especially if you are not an experienced Unix user. Historically regular expressions were originally designed to help working with strings under Unix systems.

Using regular expressions you can easy find a pattern in a string and/or replace it if you want. This is a very powerful tool in your hand, but be careful as it is slower than the standard string manipulation functions.

Regular expression types

There are 2 types of regular expressions:

* POSIX Extended
* Perl Compatible

The ereg, eregi, … are the POSIX versions and preg_match, preg_replace, … are the Perl version. It is important that using Perl compatible regular expressions the expression should be enclosed in the delimiters, a forward slash (/), for example. However this version is more powerful and faster as well than the POSIX one.

The regular expressions basic syntax

To use regular expressions first you need to learn the syntax of the patterns. We can group the characters inside a pattern like this:

* Normal characters which match themselves like hello
* Start and end indicators as ^ and $
* Count indicators like +,*,?
* Logical operator like |
* Grouping with {},(),[]

An example pattern to check valid emails looks like this:


The code to check the email using Perl compatible regular expression 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”;

And very similar in case of POSIX extended regular expressions:

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

Now let’s see a detailed pattern syntax reference:

Regular expression (pattern)

Match (subject)

Not match (subject)

world Hello world Hello Jim Match if the pattern is present anywhere in the subject
^world world class Hello world Match if the pattern is present at the beginning of the subject
world$ Hello world world class Match if the pattern is present at the end of the subject
world/i This WoRLd Hello Jim Makes a search in case insensitive mode
^world$ world Hello world The string contains only the “world”
world* worl, world, worlddd wor There is 0 or more “d” after “worl”
world+ world, worlddd worl There is at least 1 “d” after “worl”
world? worl, world, worly wor, wory There is 0 or 1 “d” after “worl”
world{1} world worly There is 1 “d” after “worl”
world{1,} world, worlddd worly There is 1 ore more “d” after “worl”
world{2,3} worldd, worlddd world There are 2 or 3 “d” after “worl”
wo(rld)* wo, world, worldold wa There is 0 or more “rld” after “wo”
earth|world earth, world sun The string contains the “earth” or the “world”
w.rld world, wwrld wrld Any character in place of the dot.
^.{5}$ world, earth sun A string with exactly 5 characters
[abc] abc, bbaccc sun There is an “a” or “b” or “c” in the string
[a-z] world WORLD There are any lowercase letter in the string
[a-zA-Z] world, WORLD, Worl12 123 There are any lower- or uppercase letter in the string
[^wW] earth w, W The actual character can not be a “w” or “W”

Now you know the basics, lets go advanced.

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”;


If you like the post & want to get more then Subscribe our blog posts. Subscribe RSS Feed Facebook Twitter Email

Both comments and pings are currently closed.

11 Responses to “PHP Regular Expressions (REGEX) tutorial for beginners”

  1. Nichol Mainello says:

    Awsome post and right to the point. I am not sure if this is actually the best place to ask but do you folks have any ideea where to employ some professional writers? Thank you 🙂

  2. Winona Ferr says:

    Nice!! Great Info. Great People. Great Blog. Thank you for all the great sharing that is being done here.

  3. Sidney Cravenho says:

    I love the expression. Everyone needs to express there own opinion and feel free to hear others. Keep it up 🙂

  4. Romana Hackett says:

    Very nice blog. Thanks for the great tips!

  5. Dwayne Zien says:

    This post is great. Thank you for this post. I like this type of people who share knowledge with others.

  6. Chrystal Broch says:

    The points you have touched here are really important. Thanks for this information. Really helpful!

  7. how to roll a joint says:

    I got what you intend, regards for posting .

  8. I'm Att says:

    Great post! My cousin recommended this blog and she was totally right keep up the fantastic work!

  9. Building Backlinks says:

    Hey how are you doing? I just wanted to stop by and say that it’s been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well. plz do keep up the quality writing

  10. Veronique Darragh says:

    Loved your tutorial. You saved me a lot of hair pulling. Honestly, thank you.

  11. Backlinks says:

    I really thought the writing in the blog was very complexed, in a good way.;)

Copyright © Sunware Technologies - Your web partner