PHP RegEx

PHP Regular Expressions (RegEx)

A regular expression (shortened as RegEx) is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are looking for.

A regular expression can be a single character, or a more complicated pattern. RegEx can be used to perform all types of text search and text replace operations, such as validating an email address or a strong password.


Syntax

In PHP, regular expressions are strings composed of delimiters, a pattern, and optional modifiers.

$exp = "/intricate/i";

In the example above, / is the delimiter, intricate is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

Common Delimiters

The delimiter can be any character that is not a letter, number, backslash, or space. The most common delimiter is the forward slash /, but when your pattern contains forward slashes, it is convenient to choose other delimiters such as # or ~.

Regular Expression Modifiers

Modifiers can change how a search is performed.

In the next chapter, we will see how to apply these patterns using PHP's built-in RegEx functions.