Introduction to single-row and multi-row Regular Expressions (usage instructions) and multiple-row Regular Expressions
Following the previous Regular Expressions (details: Regular Expressions), we will continue to discuss its single row, multi-row mode, and prone to errors. Single Row and multi-row mode are all parameters in the pattern modifier of the regular expression. Currently, this option is available for common regular expressions, such as javascript regular expressions, which are generally "/regular expression matching character/modifier". The last "/" is followed by a modifier. Then, php is similar to c #, python, etc. Generally, the matching function that calls the regular expression has another option, setting mode.
Single-row and multi-row modes are prone to comprehension errors
Why is it easy to understand errors? Their English correspondence is SingleLine, MultiLine, which is exactly the meaning of a single line and multiple lines. Therefore, many of my friends will draw the following conclusions from their literal understanding: (Haha, I just used it, and I am also a member of these friends)
1. A single line is a match from start to end. If multiple lines match a string and there is a line break in it, it will match the previous line.
2. A single row conflicts with multiple rows. Only one option can be specified at a time and cannot be used at the same time.
In this way, it is very easy to understand. Let's take a look at what the official manual says.
Official explanation of single-line and multi-line modes
Pattern character |
Description |
S (single row) |
If this modifier is set, the dot metacharacter in the mode matches all characters, including the linefeed. If this modifier is not available, the dot does not match the linefeed. |
M (multiple rows) |
The target string is composed of single-line characters (but it may actually contain multiple lines). The "first line" metacharacters (^) only match the start position of the string, the "last line" metacharacters ($) only match the end of the string. After this modifier is set, the "first line" and "last line" match any line break (\ n) in the target string before or after |
As described above, both modifiers are actually used to modify the matching range of common metacharacters in regular expressions. If the modifier "s" is added, metacharacter "is added ". "Will be able to match the line break (\ n). If the" m "modifier is added, the metacharacters" $ "will only match before the" \ n "character; the metacharacters "^" will match the "\ n" character. Let's give an example! (What about the regular expression below? Character, you can look at the previous section: Regular Expression (regex) greedy mode, lazy mode use)
For example, look at a single-row application
<? Php // read the home page of hao123.com // and remove the script code in IT/*** remove the script tag ** @ author chengmo * @ copyright http://blog.chacuo.net/* @ param string $ content original string * @ param int $ style matching mode * @ return string */function remove_script ($ content, $ style = 1) {$ reg = $ style = 1? "% <Script. *?>. *? </Script> % ":" % <script. *?>. *? </Script> % s "; return preg_replace ($ reg," ", $ content);} $ content = file_get_contents ('HTTP: // www.hao123.com '); echo remove_script ($ content );
For example, see multiline applications
<? Php // read hao123.com homepage // read meta tag content/*** read meta tag content *** @ author chengmo * @ copyright http://blog.chacuo.net/* @ param string $ content original string * @ param int $ style matching mode * @ return string */function read_meta ($ content, $ style = 1) {$ reg = $ style = 1? "% ^ <Meta .*? /> % ":" % ^ <Meta. *?> \ S + $ % m "; preg_match_all ($ reg, $ content, $ arr); return $ arr ;}$ content = file_get_contents ('HTTP: // www.hao123.com '); var_dump (read_meta ($ content ));
Note: The s and m modifiers are only correct. Several Special metacharacters have changed. If your regular expression does not contain those metacharacters. When s is enabled, there will be no changes before and after m characters. For the hao123.com Code read above, we can continue to use the s, m mode at the same time. For example: "% <script. *?>. *? (^ CurrentProfile. * $ ).*? </Script> % sm ", which matches all script tags and contains the js Code. There is a string starting with curentProfile. (The following is a regular expression, which is used together with multiple rows in a single row)