Home > Article > Backend Development > Detailed explanation of the role of pattern modifiers in regular expressions
Detailed explanation of the role of pattern modifiers in regular expressions (i, g, m, s, x, e)
What are pattern modifiers?
1. Pattern modifiers are just a few letters. We can use one at a time or multiple consecutively in each regular expression. Each one has 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 the formula of regular expressions? '/Atoms and Metacharacters/Pattern Modifier', where the forward slash is the boundary character.
The composition of pattern modifiers
Pattern modifiers are letters, but these have special meanings in the application of pattern modifiers. Let me take a look at what pattern modifiers are available. Please see the following table:
Since i means that the match is not case-sensitive, in the following example, we no longer To demonstrate, let's look at examples of other pattern modifiers.
1, mode modifier m.
The code is as follows:
<?php $pattern = '/^abc/m'; $string = 'bcd abc cba'; if (preg_match($pattern, $string, $arr)) { echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
The matching result is successful. Note: When we use the pattern modifier m, we treat the matching string as multiple lines instead of the default single line, so as long as any line starts with abc, the match will be successful. However, if there are spaces in front of the matching lines, they will not be matched! Unless the matching mode of the regular expression is modified.
2, mode modifier s.
The code is as follows:
<?php $pattern = '/a.*c/s'; $string = 'adsadsa c'; if (preg_match($pattern, $string, $arr)) { echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
This time the matching demerit was also successful. If you remove the pattern modifier s in the above example, the match will fail. Because the pattern modifier s treats the matched string as a single line, at this time, the "." in the metacharacter can represent a newline symbol.
3, mode modifier x.
The code is as follows:
<?php $pattern = '/a c/x'; $string = 'a c'; if (preg_match($pattern, $string, $arr)) { echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
The matching result this time was failed. Because we used the pattern modifier x to cancel the spaces in the pattern. Note: We cannot use the pattern modifier to cancel the white space represented by \s.
4, mode modifier A.
The code is as follows:
<?php $pattern = '/ac/A'; $string = 'acahgyghvbm'; if (preg_match($pattern, $string, $arr)) { echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
The meaning of the regular expression is to match the string starting with ac, and the result is successful.
The pattern modifier Z represents a match that ends in a string. The usage is the same as A. We will not demonstrate it again.
5, mode modifier U.
This pattern modifier is very important! In regular expressions, it itself is "greedy". So what is greedy mode? Greedy mode means that by default, the regular expression will continue to try subsequent matches after finding the first match. If a match can be found, it will match the largest range of strings. But sometimes this is not the result we want, so we need to cancel greedy mode.
Let’s look at an example of greedy mode first:
The 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 "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
The original intention of this example is to match welcome, but the result is to match the entire character of welcome to phpfuns string (note that our string 'welcome to phpfuns', its beginning and end exactly constitute the pattern matching of the regular expression, so the match is successful), this is the greedy mode of the regular expression. Of course, this is not the result we want.
Cancel the greedy mode
We can use the pattern modifier U and the metacharacter ? to cancel the greedy mode of the regular expression in two ways.
Mode modifier U cancels greedy mode
The 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 "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
Metacharacter? Cancel greedy mode
The 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 "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; print_r($arr); } else { echo "<font color='red'>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; } ?>
The above is the detailed content of Detailed explanation of the role of pattern modifiers in regular expressions. For more information, please follow other related articles on the PHP Chinese website!