PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP 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 Finishing

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 Character Description Example Explained
\ 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) ^hello Matches 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 break h.llo Matches h, then, any character and then, llo (hello, hollo, hallo, ... are matched)
| OR conditional operator hello|hallo Matches either hello or hallo
() Defines a sub-pattern hel(lo|p) Matches either hello or help
[] Defines a character class hel[a-z]o Matches hel, then, any lowercase letter and then, o (hello, helko, helpo)
{} Defines a repetition pattern hel{1,3}o Matches he, then, 1 to 3 l characters and then, o (helo, hello, helllo)
? Matches a pattern 0 or 1 time hell?o Matches hel, then, either l or nothing, then o (hello, helo)
* Matches a pattern 0 or more times hel*o Matches he, then 0 or more l characters and then, o. (heo, helo, hello, helllo)
+ Matches a pattern 1 or more times hel+o Matches 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 Character Description Example Explained
\ 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 character hel[^abc]o Matches hel, then, any character except a, b and c and finally, o. (hello, helko, helwo)
- Matches a range of characters hel[a-z]o Matches 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 Character Meaning
\t Tab Space
\n New Line
\r Carriage 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 type Description
\d Any decimal digit
\D Any character except decimal digits
\w Any word character
\W Any non-word character
\s Any whitespace character
\S Any 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 Sequence Description
\b Word boundary
\B Not 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.

Modifier Description
i Makes the match case-insensitive.
m Activates the multi-line mode.
s Allows . 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.