A regular expression is a sequence of characters that defines a search pattern. You can use this search pattern to identify specific data within a text.
A regular expression can be as simple as a single character or as complex as a detailed pattern.
Regular expressions are useful for various text search and text replacement operations.
Syntax
In PHP, regular expressions are strings consisting of delimiters, a pattern, and optional modifiers.
<?php
$exp = "/WebmasterMaze/i";
?>
In the example above, /
is the delimiter, WebmasterMaze
is the pattern being searched for, and i
is a modifier that makes the search case-insensitive.
The delimiter can be any character that is not a letter, number, backslash, or space. The most common delimiter is the forward slash (/
), but if your pattern contains forward slashes, it is convenient to choose other delimiters such as #
or ~
.
Regular Expression Functions
PHP provides a variety of functions for using regular expressions.
The most common functions are:
preg_match()
- Returns 1 if the pattern was found in the string and 0 if notpreg_match_all()
- Returns the number of times the pattern was found in the string, which may also be 0preg_replace()
- Returns a new string where matched patterns have been replaced with another string
preg_match()
The preg_match()
function determines whether a string contains matches for a pattern.
For example, you can use a regular expression to perform a case-insensitive search for "WebmasterMaze" in a string:
<?php
$str = "I love WebmasterMaze";
$pattern = "/webmastermaze/i";
echo preg_match($pattern, $str);
?>
/*
Output:
1 (Means match found)
*/
preg_match_all()
The preg_match_all()
function indicates how many matches were found for a pattern in a string.
For example, you can use a regular expression to perform a case-insensitive count of the number of occurrences of "run" in a string:
<?php
$str = "She decided to run, run, and run until she reached the finish line.";
$pattern = "/run/i";
echo preg_match_all($pattern, $str);
?>
/*
Output:
3
*/
preg_replace()
The preg_replace()
function replaces all matches of a pattern in a string with another string.
For example, you can use a case-insensitive
regular expression to replace "sunset" with "dusk" in a string.
<?php
$str = "He watched the sunset as the sunset painted the sky with colors.";
$pattern = "/sunset/i";
echo preg_replace($pattern, "dusk", $str);
?>
/*
Output:
He watched the dusk as the dusk painted the sky with colors.
*/
Regular Expression Modifiers
Modifiers can alter how a search is conducted.
i
- Performs a case-insensitive searchm
- Performs a multi-line search (patterns that search for a match at the beginning or end of a string will now match the beginning or end of each line)u
- Enables correct matching ofUTF-8
encoded patterns
Regular Expression Patterns
Brackets
Brackets are used to specify a range of characters to search for.
[abc]
- Find one or many of the characters inside the brackets[^abc]
- Find any character NOT between the brackets[a-z]
- Find any character alphabetically between two letters[A-z]
- Find any character alphabetically between a specified upper-case letter and a specified lower-case letter[A-Z]
- Find any character alphabetically between two upper-case letters.[123]
- Find one or many of the digits inside the brackets[0-5]
- Find any digits between the two numbers[0-9]
- Find any digits
Metacharacters
Metacharacters are characters that have a special meaning.
|
- Find a match for any one of the patterns separated by | as in: cat|dog|fish.
- Find any character^
- Finds a match as the beginning of a string as in: ^Hello$
- Finds a match at the end of the string as in:World$
\d
- Find any digits\D
- Find any non-digits\s
- Find any whitespace character\S
- ind any non-whitespace character\w
- Find any alphabetical letter (a to Z) and digit (0 to 9)\W
- Find any non-alphabetical and non-digit character\b
- Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b\uxxxx
- Find the Unicode character specified by the hexadecimal number xxxx
Quantifiers
Quantifiers specify the quantities of characters or patterns.
n+
- Matches any string that contains at least one nn*
- Matches any string that contains zero or more occurrences of nn?
- Matches any string that contains zero or one occurrences of nn{3}
- Matches any string that contains a sequence of 3 n'sn{2, 5}
- Matches any string that contains a sequence of at least 2, but not more that 5 n'sn{3,}
- Matches any string that contains a sequence of at least 3 n's
Note: If your expression needs to search for one of the special characters, you can use a backslash (\
) to escape them.
For example, to search for one or more question marks, you can use the following expression: $pattern = '/\?+/';
Grouping
Parentheses ()
can be used to group parts of a pattern and apply quantifiers to the entire group. Additionally, they allow you to capture portions of the pattern for later use as a match.
Use grouping to search for the word "oranges" by looking for "or" followed by one occurrence of "an".
<?php
$str = "Apples and oranges.";
$pattern = "/or(?:an){1}/i";
// Check if the pattern matches the string and output the result
if (preg_match($pattern, $str)) {
echo "Match found.";
} else {
echo "No match found.";
}
?>
/*
Output:
Match found.
*/