How to get started with regular expression in PHP

Regular expression or regex is a very vast topic. We won’t be covering it all in this shot, but it’s still possible to quickly learn something about it.

Objectives: at the end of this shot, you’ll be able to:

  • write a basic regex in PHP
  • test a regex with preg_match() function

Let’s get started.

What’s a regular expression?

A regular expression, aka regex, is a sequence of characters that represent a pattern. It is also the “language” we use to specify or program patterns for the computer to recognize.

Let’s look at a simple example.

Given the sentence below:

I teach at Educative.

If I want to check if the sentence contains the word “Educative”, I can use it as a pattern like this, /Educative/. You can test this pattern and the sentence in the widget below to see the result.

Note: a pattern is always wrapped by delimiters that can be any sign you want: /, #…

The general syntax is:

/pattern/options

An option or a flag can be, for instance, “i” to ignore the case.

Let’s learn some common patterns you can find out there.

Symbols

pattern description
\| OR, match the first word or the second one, and so on.
. matches only a single character.
^ string start anchor matches the word, but only if it’s at the beginning.
string end anchor matches the word, but only if it’s located at the end.

Now, let’s use the preg_match() function to perform a regular expression match on a given string. The syntax is as follows:

preg_match(pattern, string)

Example:

preg_match("/PHP|JS/", "I love PHP or Python")

This is how you can read the code above: search “I love PHP or Python” for a match to the regular expression given in “/PHP|JS/”.

Both pattern and string parameters are of type string.

Practical exercise

In the exercise below, we will us a ternary operator to print out true or false depending on whether or not there’s a match.

Feel free to modify the code as much as possible until you gain a better understanding of the regex.

<?php
/*
* You can add whatever you want to test regex
*/
print_r(preg_match("/PHP|JS/", "I love PHP or Python") ? "true" : "false"); // true
echo "<br>";
print_r(preg_match("/h./", "hello word!") ? "true" : "false"); // true
echo "<br>";
print_r(preg_match("/h./", "Hello word!") ? "true" : "false"); // false
echo "<br>";
print_r(preg_match("/^h./i", "Hello word!") ? "true" : "false"); // true
echo "<br>";
print_r(preg_match("/word$/", "Hello word!") ? "true" : "false"); // false
echo "<br>";
print_r(preg_match("/word.$/", "Hello word!") ? "true" : "false"); // true

Wrap-up

Regex is just a special text string used to describe a search pattern that can be any character and/or sequence of symbols (like ., *, etc) wrapped by a deliminator.

To test regex in PHP, use preg_match() as follows:

preg_match(pattern, string)

Now that you understand the regex, write your own regex for the assignment below.

Assignment

Given the strings below:

  • hello
  • hello world
  • Hello, world!
  • Hello PHP!
  • sarahmasika@abelmbula.com
  • P@55w0rd!
  • 12, 5th Avenue Makiso, Kisangani

Write a regex that:

  • matches all the words
  • matches the first four words

You can use the widget below to test your solution.

If you are really stuck somewhere, you can find the solutions below.

<?php
/*
* - change "pattern" with your actual pattern and
* - the sentence or word with the actual one
* - hit the "Run" button to see the result.
*/
print_r(preg_match("/pattern/", "Word or sentence to search for pattern") ? "true" : "false");

Solutions

Make sure that you’ve tried to find the solution by yourself before you look.

Free Resources