Cheat sheet Regular expression (regex) for preg_match() in PHP

This is a small cheat sheet that i personally use for regex to be used in PHP with the help of preg_match() function.

Escape character in regex in PHP

I personally prefer to use  / (forward slash) as delimiter in preg_match(‘/regex/’, $subject). So, in this case, if we want to use an url which has forward slash we will use \ (backward slash) as an escape character. 
For eg.,


will need to written in the following way:


In the above example you will see that we have used  \ (backward slash) as an escape character in the regex.

Using a different delimiter in regex

You have the freedom to select your own delimiter which is a non-alphabetic character in the regular expression used in the preg_match() function. Let’s take the same example and use % as a  delimiter. Now since /(forward slash )  is not used as a delimiter you do not have to escape them anymore.

This is how it will be written:






Add a Comment

Your email address will not be published. Required fields are marked *