PHP regular expression tutorial

Regular expressions are a powerful way of analyzing and retrieving values from text strings .

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:

1 Start and end indicators as ^ and $
2 Count indicators like +,*,?
3 Logical operator like |
4 Grouping with {},(),[]
5 \ is used as a general escape character

A basic example would looks like this:

<?php
if (preg_match('/tutorial/', 'tips and tutorials are very useful')) {
    echo "word 'tutorial' found!";
?>

Now let's see a detailed pattern syntax reference:

 "ab*" //matches a string that has an a followed by zero or more b's ("a", "ab", "abbb", etc.);
"ab+" //same, but there's at least one b ("ab", "abbb", etc.);
"ab?": //there might be a b or not;
"a?b+$" //a possible a followed by one or more b's ending a string.
"ab{2}" //matches a string that has an a followed by exactly two b's ("abb");
"ab{2,}" //there are at least two b's ("abb", "abbbb", etc.);
"ab{3,5}" //from three to five b's ("abbb", "abbbb", or "abbbbb").
"a(bc)*" //matches a string that has an a followed by zero or more copies of the sequence "bc";
"a(bc){1,5}" //one through five copies of "bc."
"hi|hello" //matches a string that has either "hi" or "hello" in it;
"(b|cd)ef" //a string that has either "bef" or "cdef";
"(a|b)*c" //a string that has a sequence of alternating a's and b's ending in a c;
"a.[0-9]" //matches a string that has an a followed by one character and a digit;
"^.{3}$" //a string with exactly 3 characters.
"[ab]" //matches a string that has either an a or a b  (that's the same as "a|b");
"[a-d]" //a string that has lowercase letters 'a' through 'd' (that's equal to "a|b|c|d" and even "[abcd]");
"^[a-zA-Z]" //a string that starts with a letter;
"[0-9]%" //a string that has a single digit before a percent sign;
",[a-zA-Z0-9]$" //a string that ends in a comma followed by an alphanumeric character.

------------------------------------------------------------------------------------------

An example pattern to check valid emails looks like this:

^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$

The code to check the email using Perl compatible regular expression looks like this:
<?php
  $pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
  $email = "jim@demo.com";
  if (preg_match($pattern,$email)) {
    echo "Match";
  }
   else {
     echo "Not match";
   }
?>

Hope this helps .

1 comments on PHP regular expression tutorial

  1. paula (not verified)
    Mon, 05/16/2011 - 13:22

    great tutorial :)

  2. Post new comment

    The content of this field is kept private and will not be shown publicly.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.

    More information about formatting options

    By submitting this form, you accept the Mollom privacy policy.