Home > Web Front-end > JS Tutorial > body text

Multi-line mode and single-line mode graphic analysis_regular expression

微波
Release: 2017-06-28 13:38:18
Original
1231 people have browsed it

This article mainly introduces the graphic analysis of multi-line mode and single-line mode of regular expression. Friends who need it can refer to

In Expresso, test "multi-line mode" ”

Test 1

#Note: There is no carriage return after 3eeeee in the sample text here, and the cursor is right after e. The matching result is 3eeeee, as shown in the Search Results area above.

Why can't 1abcde and 2abc match here?

Turn on multi-line mode

^ It can match the beginning of string (the starting position of the string), or it can match the beginning of the line (i.e. The position after the newline character \n)
$ can match the end of the string (the end position of the string), or the end of the line (that is, the position before the newline character \n)

Turn off multi-line mode

^ Can only match the beginning of the string
$ Can only match the end of the string

Knowledge points: \r is a carriage return character, \n is a newline symbol. In Windows, what we usually call line feed is essentially carriage return first and then line feed; there is a more detailed explanation below.

As shown in the picture above: \r matches [CR], \n matches [LF] <—— CR is carriage return LF is line feed
Multiple strings A paragraph, for example,
ab
cd
e
in Windows operating system is actually: ab[CR][LF]cd[CR][LF]e

in Windows, The carriage return and line feed in the text are stored as: 0D 0A. In other words, what is stored first is "carriage return\r", and then what is stored is "line feed\n"
CR is represented by the symbol '\r', and the ASCII code is 13, ten Hexadecimal is 0x0D;
LF is represented by the symbol '\n', ASCII code is 10, hexadecimal is 0x0A;

Regular expression: (?m)^(\d\w+ )(\s*)$

Sample text

Matching result

In Expresso . In PHP, when multi-line mode is enabled, "$" matches the end of the string or the position before "\n".

Single-line mode

Enable single-line mode: .Can match any character (including newline characters)
Turn off single-line mode: .Only match non-newline characters Other Any characters (. can match \r, that is, all characters except \n are not matched.)

Multi-line mode affects the matching of ^ and $
Single-line mode affects the matching of .

Multi-line mode must contain ^ or $ or both, otherwise even if m is added, it will have no meaning

Single-line mode and multi-line mode are two concepts that cannot be beaten with eight poles. It is only because of the historical reasons of regular development that the two concepts of MS are mutually exclusive.
The single-line mode affects the matching range of the decimal point "."
The multi-line mode affects the matching range of "^" and "$" Matching scope

As for the following concepts, there is no necessary connection between global matching, multi-line mode and greedy mode

Global matching is turned off, only the first successful match is matched, global matching is turned on , match all successful matches
Global mode is a concept only found in some scripting languages
When matching, turn off the global mode, similar to the Match method in .NET, turn on the global mode, similar to .NET The Matches method
turns off the global mode when performing a replacement, similar to replaceFirst in Java, and turns on the global mode, similar to replaceAll

in Java (when performing a match, turns off the global mode, similar to preg_ match function in PHP; turn on global mode, similar to preg_ match_ all function in PHP)

The above is the detailed content of Multi-line mode and single-line mode graphic analysis_regular expression. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!