Introduction to the use of pattern modifiers in regular expression tutorial

Time:2020-11-21

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

What is a pattern modifier?

1, pattern modifier is a few letters, we can use one at a time in each regular expression, also can use more than one, each has a certain meaning.
2. The pattern modifier is used for the whole regular expression tuning, which can also be said to be an extension of the regular expression function.
Remember the formula for regular expressions? ‘/ atom and metacharacter / pattern modifier’, where the forward slash is the boundary character.

The constitution of pattern modifier
Pattern modifiers are letters, but they have special meanings in the application of pattern modifiers. Let me take a look at the following table:

Pattern modifier explain
i Indicates that the matching between and pattern is not case sensitive
m Treat a pattern as multiple lines, and use ^ and $to indicate that any line can start or end with a regular expression
s If you do not use this mode 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 Indicates that white space in the pattern is ignored
e Regular expressions must be used in preg_ Replace can only be used when replacing the function of string
A Starts with a pattern string, equivalent to a metacharacter^
Z Ends with a pattern string, equivalent to a metacharacter$
U The characteristic of regular expression is that it is greedy. The greedy pattern can be cancelled by using this pattern modifier

Since I means that the match is case insensitive, in the following example, we won’t demonstrate it any more. Let’s look at examples of other pattern modifiers.

1, mode modifier M.

Copy codeThe code is as follows:
<?php
$pattern = ‘/^abc/m’;
$string = ‘bcd
abc
cba’;
if (preg_match($pattern, $string, $arr)) {
Echo “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

The matching result is successful. Note: when we use the pattern modifier m, we regard the matching string as multiple lines rather than the default single line. Therefore, as long as any line starts with ABC, the matching will succeed. However, if you can match a line with a space in front of it, you can’t match it! Unless you change the matching pattern of the regular expression.
2. Mode modifier s.

Copy codeThe code is as follows:
<?php
$pattern = ‘/a.*c/s’;
$string = ‘adsadsa
c’;
if (preg_match($pattern, $string, $arr)) {
Echo “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

This match was also successful. If you remove the pattern modifier s in the above example, the matching will fail. Because the pattern modifier s treats the matching string as a single line, the “.” in the metacharacter can represent a newline symbol.
3. Pattern modifier X.

Copy codeThe code is as follows:
<?php
$pattern = ‘/a c/x’;
$string = ‘a c’;
if (preg_match($pattern, $string, $arr)) {
Echo “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

The match failed this time. Because we use the pattern modifier x to remove the space in the pattern. Note: we cannot use the pattern modifier to cancel the whitespace represented by the.
4. Mode modifier a.

Copy codeThe code is as follows:
<?php
$pattern = ‘/ac/A’;
$string = ‘acahgyghvbm’;
if (preg_match($pattern, $string, $arr)) {
Echo “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

The regular expression means that the string starting with AC is matched and the result is successful.
The pattern modifier Z represents a match at the end of a string, which is the same as A. We won’t show you that.
5. Mode modifier U.
This pattern modifier is very important! In regular expressions, it is greedy. What is greedy pattern? Greedy pattern means that, by default, a regular expression will continue to try the following match after finding the first match. If a match can be found, it will match the largest range string. But sometimes it’s not what we want, so we need to get rid of greed.
Let’s start with an example of greed

Copy 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 “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

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

Cancel greedy mode

We can use the pattern modifier u and metacharacter? To cancel the greedy pattern of regular expressions.
The pattern modifier u cancels greedy mode

Copy 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 “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

Metacharacter? Cancel greedy mode

Copy 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 “the regular expression < b > {$pattern} < / b > and string < b > {$string} < / b > match successfully < br >”;
print_r($arr);
} else {
Echo “< / font >” failed to match the regular expression {$pattern} and string {$string} “;
}
?>

Pay attention to the position of metacharacters. We must end greedy mode before “” to achieve our goal of matching welcome!
In this section, we introduce the pattern modifiers in regular expressions, greedy patterns in regular expressions, and demonstrate the use of pattern modifiers in regular expressions. So far, we have learned the basic regular expression syntax. In the next section, we will introduce some common regular expressions for your reference.