SoFunction
Updated on 2025-03-03

Introduction to the usage of pattern correction characters in regular expression tutorial

Previously, we introduced delimiters, atoms and metacharacters in regular expressions, so the basic syntax of our regular expression tutorial is the pattern modifiers in regular expressions. This section will introduce to you the concept of pattern correction characters, the composition of pattern correction characters, and the demonstration of pattern correction characters combined with examples. After you finish learning the content of this section, you can fully understand regular expressions.

What is a pattern correction character?

1. The pattern correction character is a few letters. We can use one at a time in each regular expression or multiple consecutively, each with a certain meaning.
2. The pattern modifier is used to tune the entire regular expression, and can also be said to be an extension of the regular expression function.
Remember that formula for regular expression?'/atomic and metacharacter/pattern correction character', where the forward slash is the boundary character.

The composition of pattern correction symbols
Pattern correction characters are letters, but these have special meanings in the application of pattern correction characters. Let me see what pattern correction characters are available, please see the following table:

Pattern correction symbol illustrate
i Indicates that the pattern is not case sensitive
m Think of a pattern as multiple lines, using ^ and $ means that any line can start or end with a regular expression
s If this pattern is not used to correct the symbol, the "." in the metacharacter cannot represent a newline symbol by default, and the string is treated as a single line
x Ignore blanks in the pattern
e Regular expressions must be used when preg_replace replaces strings (this function will be discussed)
A Start with a pattern string, equivalent to a metachar ^
Z Ending in a pattern string, equivalent to the metacharacter $
U The characteristics of regular expressions: it is more "greedy". Use this pattern correction character to cancel the greedy mode.

Since i means matches are case-insensitive, in the following example, we will not demonstrate it anymore, let's take a look at the examples of other pattern modifiers.

1. Pattern correction character m.

Copy the codeThe code is as follows:

<?php
$pattern = '/^abc/m';
$string = 'bcd
abc
cba';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

The matching result is successful. Note: When we use the pattern correction character m, we regard the matching string as multiple lines instead of the default single line, so any line will be matched successfully as long as it starts with abc. However, if there are spaces in front of the matching line, it cannot be matched! Unless the matching pattern of the regular expression is modified.
2. Pattern correction character s.
Copy the codeThe code is as follows:

<?php
$pattern = '/a.*c/s';
$string = 'adsadsa
c';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

This match was also successful. If you remove the pattern correction character s in the above example, the match will fail. Because the pattern correction character s treats the matching string as a single line, at this time, the "." in the metacharacter can represent the newline symbol.
3. Pattern correction character x.
Copy the codeThe code is as follows:

<?php
$pattern = '/a c/x';
$string = 'a c';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

This match result failed. Because we use the pattern modifier x to cancel the spaces in the pattern. Note: We cannot use the pattern modifier to cancel the blank space represented by \s.
4. Pattern correction character A.
Copy the codeThe code is as follows:

<?php
$pattern = '/ac/A';
$string = 'acahgyghvbm';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

The meaning of the regular expression is to match a string starting with ac, and the result is successful.
The pattern correction character Z represents a matching ending with a string, and the usage is the same as A, and we will not demonstrate it anymore.
5. Pattern correction character U.
This pattern correction character is very important! In regular expressions, it is itself "greedy". So what is greedy pattern? The greedy pattern means that the regular expression will continue to try the subsequent match after finding the first match. If a match can be found, it will match the largest range string. But sometimes this is not the result we want, so we need to cancel the greed mode.
Let's first look at an example of greed mode:
Copy the codeThe code is as follows:

<?php
$pattern = '/<b>.*<\/b>/';
$string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

The original intention of this example is to match welcome, but the result matches the entire string of welcome to phpfuns (note that our string 'welcome to phpfuns', the beginning and end of which just constitute the pattern matching of the regular expression, so the match is successful). This is the greedy pattern of the regular expression. Of course, this is not the result we want.

Cancel greed mode

We can use the pattern correction character U and metacharacter? to cancel the greedy pattern of regular expressions.
Mode correction character U cancels greedy mode
Copy the codeThe code is as follows:

<?php
$pattern = '/<b>.*<\/b>/U';
$string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

Metacharacter? Cancel greedy mode
Copy the codeThe code is as follows:

<?php
$pattern = '/<b>.*?<\/b>/';
$string = '<b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas';
if (preg_match($pattern, $string, $arr)) {
echo "regular expression<b>{$pattern}</b> and string<b>{$string}</b> match successfully<br>";
print_r($arr);
} else {
echo "<font color='red'>regex{$pattern} and string{$string} failed to match</font>";
}
?>

Pay attention to the position of the metacharacter. We must end the greedy mode before "" to achieve our purpose and match welcome!
In this section, we introduce the pattern correctors in regular expressions, the greedy pattern of regular expressions, and demonstrate how to use pattern correctors in regular expressions. So far, we have also learned the basic regular expression syntax composition. In the next section, we introduce some commonly used regular expressions for your reference.