Following a few more regular expressions (details: Regular expressions), we continue today to discuss the use of its single line, multiline mode, and prone to error places. A single line, multiline mode, is a parameter that appears in the pattern modifier of a regular expression. Current regular expressions have this use option, such as: JavaScript regular expressions, usually: "/regular expression matching character/modifier", the Last "/" followed by a modifier. Then, PHP is similar, C#,python, and so on, generally call the regular expression of the matching function, all have a different option, set the mode.
Single line, multiline mode easy to understand error
Why, it is easy to understand errors, their English counterpart is: Singleline, MultiLine, just a one-way, multiline meaning. Therefore, a lot of friends will be literal understanding, draw the following conclusions: (Haha, just use, I am also a member of these friends)
1, a single line, is to match from beginning to end, many lines is if the matching string, there are line breaks, match to the previous
2. A single line is conflicting with multiple lines, only one option can be specified at a time and cannot be used at the same time
In this way, it's easy to understand. Let's take a look at what the Official Handbook says.
Single line, multiline mode official explanation
Pattern Character |
Description |
S (single line) |
If this modifier is set, the dot character in the pattern matches all characters, including line breaks. If you do not have this modifier, the dot number does not match the newline character |
m (Multiple lines) |
The target string is made up of a single line of characters (however it may actually contain multiple lines), and the "line Start" metacharacters (^) match only the beginning of the string, and the "End" Metacharacters ($) match only the end of the string. When this modifier is set, the "Beginning" and "end" matches any newline character (\ n) before or after the target string |
As explained above, in fact, these 2 modifiers are just, modify the regular expression common metacharacters of the match range. If you add the "s" modifier, the meta character "." will be able to match the newline character (\ n), and if the "m" modifier is added, the metacharacters "$" will only match to the "\ n" character match either, and the metacharacters "^" will match to the "\ n" character End-of-file. Let's give you an example. (for the regular expression? character, see the previous section: Regular expression (regex) greedy mode, lazy mode use)
For example, see Single Application
<?php
///Read hao123.com home
///and remove script code
/** *
Remove Script tags * *
@author Chengmo
* Copyright http://blog.chacuo.net/
* @param string $content The original string
* @param int $style match 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 Multiple lines of application
<?php
///Read hao123.com home
///read meta tag content
/**
* Read META tag content * *
@author Chengmo
* @copyright http://blog.chacuo.net/
* @param string $content The original string
* @param int $style match 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));
PostScript: S,m modifier only, several special meta characters have change. If you don't have that few metacharacters in your regular expression. There will be no change before and after the s,m character is opened. For reading hao123.com code above, we can continue to use S,m mode at the same time. such as: "%<script.*?>.*?" (^currentprofile.*$). *?</script>%sm ", matching all script tags, and inside the JS code, there is a line to curentprofile the beginning of the string. (The following are regular expressions, which are used together in single lines)