Detailed explanation of the role of pattern modifiers in regular expressions

藏色散人
Release: 2023-04-08 10:42:02
forward
3148 people have browsed it

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:

Detailed explanation of the role of pattern modifiers in regular expressions

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 = &#39;/^abc/m&#39;; 
$string = &#39;bcd 
abc 
cba&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

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 = &#39;/a.*c/s&#39;; 
$string = &#39;adsadsa 
c&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

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 = &#39;/a c/x&#39;; 
$string = &#39;a c&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

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 = &#39;/ac/A&#39;; 
$string = &#39;acahgyghvbm&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

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 = &#39;/<b>.*<\/b>/&#39;; 
$string = &#39;<b>welcome</b> <b>to</b> <b>phpfuns</b>&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

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 = &#39;/<b>.*<\/b>/U&#39;; 
$string = &#39;<b>welcome</b> <b>to</b> <b>phpfuns</b>&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

Metacharacter? Cancel greedy mode

The code is as follows:

<?php 
$pattern = &#39;/<b>.*?<\/b>/&#39;; 
$string = &#39;<b>welcome</b> <b>to</b> <b>phpfuns</b>dsadsadas&#39;; 
if (preg_match($pattern, $string, $arr)) { 
echo "正则表达式<b>{$pattern}</b>和字符串<b>{$string}</b>匹配成功<br>"; 
print_r($arr); 
} else { 
echo "<font color=&#39;red&#39;>正则表达式{$pattern}和字符串{$string}匹配失败</font>"; 
} 
?>
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template