Home  >  Article  >  Web Front-end  >  Detailed graphic and text explanation of using regular multi-line mode and single-line mode

Detailed graphic and text explanation of using regular multi-line mode and single-line mode

php中世界最好的语言
php中世界最好的语言Original
2018-03-29 16:14:022058browse

This time I will bring you detailed graphic and text explanations of using regular multi-line mode and single-line mode. What are the precautions when using regular multi-line mode and single-line mode. The following is a practical case. Let’s take a look. one time.

In Expresso, test "multiline mode"

Test one

Note: Here is a sample There is no carriage return after 3eeeeee in the text, 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 and 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 the global mode, similar to the preg_ match_ all function in PHP)

I believe you have mastered the method after reading the case in this article. For more exciting things, please pay attention to php Other related articles on the Chinese website!

Recommended reading:

Detailed explanation of the use of PHP regular zero-width assertions

How to implement the fuzzy matching function of regular expressions

The above is the detailed content of Detailed graphic and text explanation of using regular multi-line mode and single-line mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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