Regular expression Single-line, multiline pattern introduction (using instructions) _ Regular expressions

Source: Internet
Author: User
Tags modifier

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

///Read home
///and remove script code
/** *
 Remove Script tags * *
 @author Chengmo
 * Copyright
 * @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 (' ');
echo Remove_script ($content);

For example, see Multiple lines of application

///Read home
///read meta tag content
 * Read META tag content * *
 @author Chengmo
 * @copyright
 * @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 (' ');
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 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.