What is a Regular Expressions?

Regular expressions are powerful pattern matching algorithm that can be performed in a single expression.

Regular expressions use arithmetic operators such as (+,-,^) to create complex expressions.

Regular expressions help you accomplish tasks such as validating email addresses, IP address etc.

Why to use regular expressions

  • Regular expressions simplify identifying patterns in string data by calling a single function. This saves us coding time.
  • When validating user input such as email address, domain names, telephone numbers, IP addresses,
  • Highlighting keywords in search results
  • When creating a custom HTML template. Regular expressions can be used to identify the template tags and replace them with actual data.

In this tutorial, you will learn-

Regular expressions in PHP

PHP has built in functions that allow us to work with regular functions. Let’s now look at the commonly used regular expression functions in PHP.

  • preg_match – this function is used to perform a pattern match on a string. It returns true if a match is found and false if a match is not found.
  • preg_split – this function is used to perform a pattern match on a string and then split the results into a numeric array
  • preg_replace – this function is used to perform a pattern match on a string and then replace the match with the specified text.

Below is the syntax for a regular expression function such as preg_match,preg_split or preg_replace.




  • "function_name(...)" is either preg_match, preg_split or preg_replace.
  • "/.../" The forward slashes denote the beginning and end of our regular expression
  • "'/pattern/'" is the pattern that we need to matched
  • "subject" is the text string to be matched against

Let’s now look at practical examples that implement the above regular expression functions in PHP.

PHP Preg_match

The first example uses the preg_match function to perform a simple pattern match for the word guru in a given URL.

The code below shows the implementation for the above example.

$my_url = "www.guru99.com";
if (preg_match("/guru/", $my_url))
echo "the url $my_url contains guru";
echo "the url $my_url does not contain guru";

  Browse to the URL http://localhost/phptuts/preg_match_simple.php

PHP Regular Expressions

Let’s examine the part of the code responsible for our output "preg_match('/guru/', $my_url)"   HERE,

  • "preg_match(...)" is the PHP regular expression function
  • "'/guru/'" is the regular expression pattern to be matched
  • "$my_url" is the variable containing the text to be matched against.

The diagram below summarizes the above points  

PHP Preg_split

Let’s now look at another example that uses the preg_split function.

We will take a string phrase and explode it into an array; the pattern to be matched is a single space.

The text string to be used in this example is "I Love Regular Expressions".

The code below illustrates the implementation of the above example.


$my_text="I Love Regular Expressions";

$my_array  = preg_split("/ /", $my_text);

print_r($my_array );


  Browse to the URL http://localhost/phptuts/preg_split.php

PHP Regular Expressions

PHP Preg_replace

Let’s now look at the preg_replace function that performs a pattern match and then replaces the pattern with something else.

The code below searches for the word guru in a string.

It replaces the word guru with the word guru surrounded by css code that highlights the background colour.


$text = "We at Guru99 strive to make quality education affordable to the masses. Guru99.com";

$text = preg_replace("/Guru/", '<span style="background:yellow">Guru</span>', $text);

echo $text;



  Assuming you have saved the file preg_replace.php, browser to the URL http://localhost/phptuts/preg_replace.php

PHP Regular Expressions

Meta characters

The above examples used very basic patterns; metacharacters simply allow us to perform more complex pattern matches such as test the validity of an email address. Let’s now look at the commonly used metacharacters.

Metacharacter Description Example
. Matches any single character except a new line /./ matches anything that has a single character
^ Matches the beginning of or string / excludes characters /^PH/ matches any string that starts with PH
$ Matches pattern at the end of the string /com$/ matches guru99.com,yahoo.com Etc.
* Matches any zero (0) or more characters /com*/ matches computer, communication etc.
+ Requires preceding character(s) appear at least once /yah+oo/ matches yahoo
\ Used to escape meta characters /yahoo+\.com/ treats the dot as a literal value
[...] Character class /[abc]/ matches abc
a-z Matches lower case letters /a-z/ matches cool, happy etc.
A-Z Matches upper case letters /A-Z/ matches WHAT, HOW, WHY etc.
0-9 Matches any number between 0 and 9 /0-4/ matches 0,1,2,3,4

  The above list only gives the most commonly used metacharacters in regular expressions.

Let’s now look at a fairly complex example that checks the validity of an email address.

$my_email = "name @ company . com"
if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/", $my_email)) {
echo "$my_email is a valid email address";
  echo "$my_email is NOT a valid email address";

Output : This email address is being protected from spambots. You need JavaScript enabled to view it. is a valid email address  

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


  • "'/.../'" starts and ends the regular expression
  • "^[a-zA-Z0-9._-]" matches any lower or upper case letters, numbers between 0 and 9 and dots, underscores or dashes.
  • "+@[a-zA-Z0-9-]" matches the @ symbol followed by lower or upper case letters, numbers between 0 and 9 or dashes.
  • "+\.[a-zA-Z.]{2,5}$/" escapes the dot using the backslash then matches any lower or upper case letters with a character length between 2 and 5 at the end of the string.

Browse to the URL http://localhost/phptuts/preg_match.php

PHP Regular Expressions

As you can see from the above example breakdown, metacharacters are very powerful when it comes to matching patterns.


  • A regular expression is a pattern match algorithm
  • Regular expressions are very useful when performing validation checks, creating HTML template systems that recognize tags etc.
  • PHP has built in functions namely preg_match,preg_split and preg_replace that support regular expressions.
  • Metacharacters allow us to create complex patterns