


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!

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
