Getting Started with PHP Regular Expressions

August 24th, 2009

1. What are Regular Expressions

The main purpose of regular expressions, also called regex or regexp, is to efficiently search for patterns in a given text. These search patterns are written using a special format which a regular expression parser understands.

Regular expressions are originating from Unix systems, where a program was designed, called grep, to help users work with strings and manipulate text. By following a few basic rules, one can create very complex search patterns.

As an example, let’s say you’re given the task to check wether an e-mail or a telephone number has the correct form. Using a few simple commands these problems can easily be solved thanks to regular expressions. The syntax doesn’t always seems straightforward at first, but once you learn it, you’ll realize that you can do pretty complex searches easily, just by typing in a few characters and you’ll approach problems from a different perspective.

2. Perl Compatible Regular Expressions

PHP has implemented quite a few regex functions which uses different parsing engines. There are two major parser in PHP. One called POSIX and the other PCRE or Perl Compatible Regular Expression.

The PHP function prefix for POSIX is ereg_. Since the release of PHP 5.3 this engine is deprecated, but let’s have a look at the more optimal and faster PCRE engine.

In PHP every PCRE function starts with preg_ such as preg_match or preg_replace. You can read the full function list in PHP’s documentation.

3. Basic Syntax

To use regular expressions first you need to learn the syntax. This syntax consists in a series of letters, numbers, dots, hyphens and special signs, which we can group together using different parentheses.

In PHP every regular expression pattern is defined as a string using the Perl format. In Perl, a regular expression pattern is written between forward slashes, such as /hello/. In PHP this will become a string, ‘/hello/’.

Now, let’s have a look at some operators, the basic building blocks of regular expressions

Operator Description
^ The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted
$ Same as with the circumflex symbol, the dollar sign marks the end of a search pattern
. The period matches any single character
? It will match the preceding pattern zero or one times
+ It will match the preceding pattern one or more times
* It will match the preceding pattern zero or more times
| Boolean OR
- Matches a range of elements
() Groups a different pattern elements together
[] Matches any single character between the square brackets
{min, max} It is used to match exact character counts
\d Matches any single digit
\D Matches any single non digit caharcter
\w Matches any alpha numeric character including underscore (_)
\W Matches any non alpha numeric character excluding the underscore character
\s Matches whitespace character

As an addition in PHP the forward slash character is escaped using the simple slash \. Example: ‘/he\/llo/’

To have a brief understanding how these operators are used, let’s have a look at a few examples:

Example Description
‘/hello/’ It will match the word hello
‘/^hello/’ It will match hello at the start of a string. Possible matches are hello or helloworld, but not worldhello
‘/hello$/’ It will match hello at the end of a string.
‘/he.o/’ It will match any character between he and o. Possible matches are helo or heyo, but not hello
‘/he?llo/’ It will match either llo or hello
‘/hello+/’ It will match hello on or more time. E.g. hello or hellohello
‘/he*llo/’ Matches llo, hello or hehello, but not hellooo
‘/hello|world/’ It will either match the word hello or world
‘/(A-Z)/’ Using it with the hyphen character, this pattern will match every uppercase character from A to Z. E.g. A, B, C…
‘/[abc]/’ It will match any single character a, b or c
‘/abc{1}/’ Matches precisely one c character after the characters ab. E.g. matches abc, but not abcc
‘/abc{1,}/’ Matches one or more c character after the characters ab. E.g. matches abc or abcc
‘/abc{2,4}/’ Matches between two and four c character after the characters ab. E.g. matches abcc, abccc or abcccc, but not abc

Besides operators, there are regular expression modifiers, which can globally alter the behavior of search patterns.

The regex modifiers are placed after the pattern, like this ‘/hello/i’ and they consists of single letters such as i which marks a pattern case insensitive or x which ignores white-space characters. For a full list of modifiers please visit PHP’s online documentation.

The real power of regular expressions relies in combining these operators and modifiers, therefore creating rather complex search patterns.

4. Using Regex in PHP

In PHP we have a total of nine PCRE functions which we can use. Here’s the list:

  • preg_filter – performs a regular expression search and replace
  • preg_grep – returns array entries that match a pattern
  • preg_last_error – returns the error code of the last PCRE regex execution
  • preg_match – perform a regular expression match
  • preg_match_all – perform a global regular expression match
  • preg_quote – quote regular expression characters
  • preg_replace – perform a regular expression search and replace
  • preg_replace_callback – perform a regular expression search and replace using a callback
  • preg_split – split string by a regular expression

The two most commonly used functions are preg_match and preg_replace.

Let’s begin by creating a test string on which we will perform our regular expression searches. The classical hello world should do it.

$test_string = 'hello world';

If we simply want to search for the word hello or world then the search pattern would look something like this:

preg_match('/hello/', $test_string);
preg_match('/world/', $test_string);

If we wish to see if the string begins with the word hello, we would simply put the ^ character in the beginning of the search pattern like this:

preg_match('/^hello/', $test_string);

Please note that regular expressions are case sensitive, the above pattern won’t match the word hElLo. If we want our pattern to be case insensitive we should apply the following modifier:

preg_match('/^hello/i', $test_string);	

Notice the character i at the end of the pattern after the forward slash.

Now let’s examine a more complex search pattern. What if we want to check that the first five characters in the string are alpha numeric characters.

preg_match('/^[A-Za-z0-9]{5}/', $test_string);

Let’s dissect this search pattern. First, by using the caret character (^) we specify that the string must begin with an alpha numeric character. This is specified by [A-Za-z0-9].

A-Z means all the characters from A to Z followed by a-z which is the same except for lowercase character, this is important, because regular expressions are case sensitive. I think you’ll figure out by yourself what 0-9 means.

{5} simply tells the regex parser to count exactly five characters. If we put six instead of five, the parser wouldn’t match anything, because in our test string the word hello is five characters long, followed by a white-space character which in our case doesn’t count.

Also, this regular expression could be optimized to the following form:

preg_match('/^\w{5}/', $test_string);

\w specifies any alpha numeric characters plus the underscore character (_).

6. Useful Regex Functions

Here are a few PHP functions using regular expressions which you could use on a daily basis.

Validate e-mail. This function will validate a given e-mail address string to see if it has the correct form.

function validate_email($email_address)
    if( !preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+
                     ([a-zA-Z0-9\._-]+)+$/", $email_address))
        return false;
    return true;

Validate a URL

function validate_url($url)
    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?
                      (/.*)?$|i', $url);

Remove repeated words. I often found repeated words in a text, such as this this. This handy function will remove such duplicate words.

function remove_duplicate_word($text)
    return preg_replace("/s(w+s)1/i", "$1", $text);

Validate alpha numeric, dashes, underscores and spaces

function validate_alpha($text)
    return preg_match("/^[A-Za-z0-9_- ]+$/", $text);

Validate US ZIP codes

function validate_zip($zip_code)
    return preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip_code);	

7. Regex Cheat Sheet

Because cheat sheets are cool nowadays, below you can find a PCRE cheat sheet that you can run through quickly anytime you forget something.

Meta Characters

^ Marks the start of a string
$ Marks the end of a string
. Matches any single character
| Boolean OR
() Group elements
[abc] Item in range (a,b or c)
[^abc] NOT in range (every character except a,b or c)
\s White-space character
a? Zero or one b characters. Equals to a{0,1}
a* Zero or more of a
a+ One or more of a
a{2} Exactly two of a
a{,5} Up to five of a
a{5,10} Between five to ten of a
\w Any alpha numeric character plus underscore. Equals to [A-Za-z0-9_]
\W Any non alpha numeric characters
\s Any white-space character
\S Any non white-space character
\d Any digits. Equals to [0-9]
\D Any non digits. Equals to [^0-9]

Pattern Modifiers

i Ignore case
m Multiline mode
S Extra analysis of pattern
u Pattern is treated as UTF-8

8. Useful Readings

Author: Joel Reyes

Joel Reyes Has been designing and coding web sites for several years, this has lead him to be the creative mind behind Looney Designer a design resource and portfolio site that revolves around web and graphic design.

Write for Us! We are looking for exciting and creative articles, if you want to contribute, just send us an email.

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.


55 comments for „Getting Started with PHP Regular Expressions
  1. Ben on August 24th, 2009 at 8:01 am

    Great article there :)

    I think regular expressions are something you will learn over time, not something you can be done with after 1 tutorial.

  2. Gman on August 24th, 2009 at 9:58 am

    I think you have some mistakes

    ‘/he?llo/’ It will match either hllo or hello
    ‘/hello+/’ It will match hell and o on or more time. E.g. hello or helloooooooooo
    ‘/he*llo/’ Matches hllo, hello or heeello, but not hellooo
    ‘/hello|world/’ It will either match the word helloorld or hellworld
    there might be some more, but I haven’t read all

    • Joel Reyes on August 26th, 2009 at 11:54 am


      I tested the regular expressions again.

      You’re right mentioned conditions are correct.

      The last one should be condition should be like this ‘/(hello)|(world)/’ then it will match either hello or world.

      Regards :)

  3. Ivan Miši? on August 24th, 2009 at 12:36 pm

    Good explained, but after this tutorial, I’ll need lot of practice.

  4. Ahmed on August 24th, 2009 at 12:50 pm

    Very good tut ill try some tips of it

    • shad on June 14th, 2010 at 1:58 am

      hi all

      i am planing to do my final year projcet in UNIV can u HElP me with PHP please if you can email me on shad24×

  5. ed on August 24th, 2009 at 4:34 pm

    great! ive been looking for such a guide for a while now!
    thanks so much!

  6. S.Krol on August 24th, 2009 at 5:38 pm

    Try the RegexCoach

  7. joyoge designers' bookmark on August 24th, 2009 at 6:27 pm

    useful tutorial, thanks for tips..

  8. fly2279 on August 24th, 2009 at 8:37 pm

    Thank you for this great article. What theme or style are you using for your code editor?

    • Zac on August 25th, 2009 at 8:44 pm

      +1… what editor/theme is that is looks really easy on the eyes.

  9. derschreckliche on August 25th, 2009 at 2:02 am

    @Gman: I had the same thoughts reading it – so here are the right versions:
    (You just have to add bracets to match a quantifier ( ? , * , + , {min, max} ) or | (union) on more than one character.)

    ‘/(he)?llo/’ It will match either llo or hello
    ‘/(hello)+/’ It will match hello on or more time. E.g. hello or hellohello
    ‘/(he)*llo/’ Matches llo, hello or hehello, but not hellooo
    ‘/(hello)|(world)/’ It will either match the word hello or world

    here is a very useful website to test regexp online:

    Even it’s a german site it’s the best i know – for all of you who need practice, like me sometimes ;)
    Checking “Color-Pattern anzeigen” highlights your regular expression with colors and “Treffer anzeigen” will highlight the matches in the text you entered.
    The resulting $matches array is also shown.
    (Enter the regexp only, without the ‘/ and /’ )

  10. RCKY on August 25th, 2009 at 3:43 am

    Thank you…
    dont think the if statement in the email validator function is needed… return preg_match(…); would be enough… wouldn’t it?

  11. Alex on August 25th, 2009 at 4:26 am
  12. Sky on August 25th, 2009 at 1:02 pm

    PHP has some integrated functions to validate things like url’s or email’s :
    I know that this is a tutorial, but still, why code something that already exists. :) Nice day to all.

  13. Pierre on August 25th, 2009 at 3:12 pm

    Great article

    For the 2 first example of useful regex ( Email and Url ), the best way to validate thata is to use the filter_var() function from the filter extension activated in php5.2.0

  14. Andy Walpole on August 25th, 2009 at 3:18 pm

    When you first look at a regex pattern you think, “Oh my god that looks ridiculously complicated”

    But it’s not actually as complex as it initially looks.

    They are well worth learning about as they really come in handy in PHP and mod_rewrite

  15. Arkh on August 25th, 2009 at 7:32 pm

    Woot, another regex tutorial for newbies.
    Nothing about comments in the regex (yes, you can do that and it’s freakin’ usefull for big ones).
    Awesome, slash delimiters only, guess ‘`/*`’ doesn’t work. Snap.

    Why lose time writing about simple regexp when you can just link to and write about php specific topics like preg_quote which helps avoid using things like \\\ in your regexp.

  16. Keith on August 25th, 2009 at 8:37 pm

    Not a bad introduction, except for the mistakes pointed out by Gman above (see, they’re tricky things, regexes!)

    Also, don’t use the example shown for email validation — it’s too simple, and will fail valid addresses such as To validate addresses against all the forms permitted by the relevant RFCs requires a MUCH more complex regular expression.

  17. fx15 lida y?lan ya?? kar?nca yumurtas? xacc on August 25th, 2009 at 9:09 pm

    thank you. very good post.

  18. Alex Edwards on August 25th, 2009 at 11:25 pm

    Nice post.

    Might be worth mentioning some off the online testing tools? I found these invaluable when learning regexp. Here’s a couple:

  19. Bijay Rungta (@rungss), Sharing Knowledge on August 31st, 2009 at 12:02 am

    Very nicely written…

    Hope this helps People who have difficulty understanding the concept of Regular Expressions and using them in PHP..

    @rungss on Twitter

  20. Hamranhansen on September 16th, 2009 at 8:16 am

    > As an addition in PHP the forward slash character
    > is escaped using the simple slash \.

    This is a “slash”: /

    This is a “backslash”: \

  21. Ahmad Alfy on September 19th, 2009 at 5:56 am

    On Pattern Modifiers, you forgot g (global)
    It’s pretty useful

  22. Mehedi Hasan on November 17th, 2009 at 9:00 am

    great great great tutorial. today i learn many things… thanks a lot for sharing…

  23. krisha on November 25th, 2009 at 7:05 am

    good tutorial i improved my knowledge

  24. Kian Ann on February 1st, 2010 at 10:05 am

    This is a great tutorial! But I don’t get the remove duplicate words thing?

    return preg_replace(“/s(w+s)1/i”, “$1″, $text);

    I’d assume the text is “this this”, and we “/s(w+s)1/i” is aupposed to match “this this” and replace with “this”?

    Does the second part “$1″ refers to the (w+s) group? How do we match two identical words?

    • Kian Ann on February 1st, 2010 at 5:24 pm

      Further to my confusion, I’ve went to test the replace duplicate words regex out and it doesn’t work… at least not for me.

      return preg_replace(”/s(w+s)1/i”, “$1?, $text);

      I went around doing some searches and ended up asking a friend. This works to replace duplicate words:

      return preg_replace(“/(\w+)\s(\\1)/”, “$1″, “Oh this this is good”);
      // returns “Oh this is good”

      But this replaces only 1 duplicate word. If the text was “Oh this this this is good”, then it will not replace 3 copies of the words at once – you’ll have to do an iteration (or recursion)… whatever u call it.

  25. chandan dutta on March 15th, 2010 at 11:54 pm

    web designer
    Thanks for great article.Its very useful for me.

  26. mehdy on April 22nd, 2010 at 7:32 am

    Thank you for this clean and great tutorial,
    Best Regards :)

  27. bem km ipb on June 13th, 2010 at 9:00 pm

    “REGEX” is Oke.. :D

  28. zakki on June 26th, 2010 at 3:44 am

    basic syntax help me very much

  29. Kojeje on July 16th, 2010 at 11:54 pm

    Hey Master..
    thanks for teaching me this code…

  30. Who Knows on November 1st, 2010 at 1:14 pm

    Nice article, also you should consider using the filter_var php function in some cases:

    It can validate email, urls etc. without using regular expressions.


  31. Prakash on December 4th, 2010 at 2:15 am

    I think this is an effective & useful post for the professional designers to boost up their creativity. Simple to read & understand & easy to work out. We need this actually. Thanks for giving your advice at a right time. Keep on doing this great job & thx again.

  32. Denker on December 31st, 2010 at 8:29 am

    Great article, thanks for share it.

  33. Donald on January 6th, 2011 at 9:27 am

    But I’m wandering where to register.

  34. Ahmed on February 14th, 2011 at 9:33 pm

    Before reading this I knew nothing about regex (except the name), now I am leaving this page with lot of info plus good example to be kept in mind whenever dealing Regex.
    Thanks a lot.

  35. Claude Lomack on March 13th, 2011 at 5:54 pm

    Gibt es da noch Alternativen?

  36. Brett Widmann on April 24th, 2011 at 3:03 pm

    This was a really helpful guide. I will be showing this article to some other beginners to give them a basis behind PHP.

  37. Jatin on April 30th, 2011 at 8:10 am

    I was thinking of learning regular expression, thanks for showing up the article at a perfect time.

  38. Lemon on May 14th, 2011 at 5:39 am

    I want to make a program which will read a big string and search all the words that started with “C” and Ending with “T” by PHP.

    Please Guide me , how to?

  39. Neeraj on May 24th, 2011 at 10:54 am

    Hey Lemon, I think the following may work for you

    $pattern = '/C.*T/';
    $string = "A cat tear a coat";

    if (preg_match($pattern, $string)) {
    echo "Valid String”;
    echo “Invalid String”;

  40. Valmir on June 11th, 2011 at 10:37 pm

    I need help on this –> ^N[[:alpha:]]*{-8}[0-9] what is the rezult?

  41. meeni on February 7th, 2012 at 11:13 am

    great article..thanks for the help..

  42. Gerard on March 28th, 2012 at 1:55 am

    Thanks a lot for this. Really helped me out. I hate Regular Expressions!!!!!

  43. Akash on August 10th, 2012 at 9:35 am

    Quit helpful for the beginners…

  44. nothingchen on October 13th, 2012 at 2:42 pm

    very useful. thank you so much. :)

    Follow me on twitter:

  45. Md Aijaz on December 18th, 2012 at 12:30 pm

    awesome. thanks

  46. Web Genius on March 28th, 2013 at 12:03 am

    Woww, i love this tutorial, it really helped:-)

  47. laeeq on July 28th, 2013 at 9:06 am

    Thanks a lot for this. Really helped me out.

  48. Nalaka Prasad on August 6th, 2013 at 7:56 pm

    Thanks for the greate tut about regular expressions, I learned a lot abut regexp from this article.. :)

  49. alen on January 24th, 2014 at 8:31 am

    Nice tutorial. Thank you.

  50. hvardhan on February 7th, 2014 at 1:14 pm

    nice tutorial :)
    Try php based online regex editor which helps you test your regular expressions with real-time highlighting of regex match on data input