PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP Object Oriented ProgrammingPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP Interfaces

Regex PCRE Syntax


When using PCRE functions in PHP, it is required to enclose the pattern by delimiters.

A delimiter can be any character except following.

Often used delimiters: /, #, ~

/hello world/
#hello world#
~hello world~

You can also use brackets as delimiters where the opening and closing brackets represents the beginning and ending respectively.

Valid bracket style delimiters: (), [], {}, <>

(hello world)
[hello world]
{hello world}
<hello world>

If the delimiter need to be matched inside the pattern, you should escape the delimiter by \ (back slash). If the delimiter appears several times in a pattern, it is better to use another delimiter.
  • bad: /http:\/\//
  • good: #http://#
But, brackets style delimiters do not need to be escaped.
Note: You will learn more about escaping with \ in next sub topics of this chapter.

Meta Characters

Meta characters are helpers for complicated patterns. They do not stand for themselves, but empower the regex to do more.

There are two types of meta characters because of their different behavior when they are inside and outside square brackets [].

Outside square brackets

Meta CharacterDescriptionExampleExplained
\Used for escape the next character.\.. matches any character while \.matches dot. The full regex will match any character followed by a dot.
^Matches the start of a string (or line, in multi-line mode)^helloMatches the word hello which is in the beginning of a string.
$Matches the end of a string (or end of line, in multi-line mode)hello$Matches the word hello which is in the ending of a string.
.Matches any character except line breakh.lloMatches h, then, any character and then, llo (hello, hollo, hallo, ... are matched)
|OR conditional operatorhello|halloMatches either hello or hallo
()Defines a sub-patternhel(lo|p)Matches either hello or help
[]Defines a character classhel[a-z]oMatches hel, then, any lowercase letter and then, o (hello, helko, helpo)
{}Defines a repetition patternhel{1,3}oMatches he, then, 1 to 3 l characters and then, o (helo, hello, helllo)
?Matches a pattern 0 or 1 timehell?oMatches hel, then, either l or nothing, then o (hello, helo)
*Matches a pattern 0 or more timeshel*oMatches he, then 0 or more l characters and then, o. (heo, helo, hello, helllo)
+Matches a pattern 1 or more timeshel+oMatches he, then 1 or more l characters and then, o. (helo, hello, helllo)

A part of pattern that is inside square brackets is called a character class. In a character class there are only three meta characters.

Inside square brackets

Meta CharacterDescriptionExampleExplained
\Used for escape the next character.\.. matches any character while \.matches dot. The full regex will match any character followed by a dot.
^Negates the class, if it is the first characterhel[^abc]oMatches hel, then, any character except a, b and c and finally, o. (hello, helko, helwo)
-Matches a range of charactershel[a-z]oMatches hel, then, any a to z character (lowercase letter) and finally, o. (hello, helko, helwo).
If you match meta characters in a pattern, you should escape them with \
  • * - Works as a meta character
  • \* - Matches *

Escape Sequences

Backslash \ is the basic escape character in PHP. It has several uses:

Escaping Meta Characters

As we learned previously, * is a meta character. If we need to match a *, we have to escape it.

In this example, * characters are replaced with $.

Escaping Meta Characters Example

$str = 'H*H*H*H*';
$regex = '/\*/';
echo preg_replace($regex, '$', $str); // returns H$H$H$H$

Run Example ››

Tip: \ is also a meta character. To match \, use \\!

Escaping Normal Characters

Escaping normal characters can make two results, special characters and generic character types.

1. Special Characters

Among plenty of special characters, we will use following characters frequently.

Special CharacterMeaning
\tTab Space
\nNew Line
\rCarriage Return

In the following example, \n (New line) is replaced with - .

Special Characters Example

$str = 
$regex = '/\n/';
echo preg_replace($regex, ' - ', $str); // returns H$H$H$H$ 

Run Example ››

2. Generic Character Types

A Generic character type is a set of characters which is really helpful in regex patterns.

Generic character typeDescription
\dAny decimal digit
\DAny character except decimal digits
\wAny word character
\WAny non-word character
\sAny whitespace character
\SAny non-whitespace character

Generic Character Types Example

$str = '06:45:12';
$regex = '/\d/';
echo preg_replace($regex, 'n', $str); // returns "nn:nn:nn"

echo "<br>";

$str = 'How are you?';
$regex = '/\s/';
echo preg_replace($regex, '', $str); // returns "Howareyou?"

Run Example ››

There are two more useful escape sequences which we will use in later tutorials.

Escape SequenceDescription
\bWord boundary
\BNot word boundary


Modifiers can modify the way that the pattern is working. As an example, if you match "hello", "Hello" will not match. To perform a case-insensitive regex, we can use the i modifier.

iMakes the match case-insensitive.
mActivates the multi-line mode.
sAllows . to match all the characters, including new lines.

Modifiers Example

$str = 'Hello World';

// outputs "Hello World"
echo preg_replace('/hello/', 'Hi', $str); 

echo '<br>';

// outputs "Hi World"
echo preg_replace('/hello/i', 'Hi', $str); 

Run Example ››
In PHP, modifiers are defined after the ending delimiter of the pattern.
Facebook Twitter