The multi-row mode and single-row mode graph analysis of the regular expression, the regular expression multiple rows

Source: Internet
Author: User
Tags preg

The multi-row mode and single-row mode graph analysis of the regular expression, the regular expression multiple rows

In Expresso, test "multiline mode"

Test 1

Note: In this example, 3eeeee is not followed by a carriage return, and the cursor is behind e. The matching result is 3 eeeee, as shown in the Search Results area.

Why cannot I match 1abcde and 2abc?

Enable multiline Mode

^ Matches the start of a string (the start position of a string) or the beginning of a line (that is, the position after the line break \ n)
$ Can match the end of a string (the end position of a string) or the end of a line (that is, the position before the line break \ n)

Disable multiline Mode

^ Only matches the start of a string
$ Only matches the end of a string

Knowledge Point: \ r is the carriage return and \ n is the linefeed. In windows, we usually say that we press enter first and then use a new line. Here is a more detailed explanation.

As shown in: \ r match [CR], \ n match [LF] <-- CR is carriage return LF is line feed
Multiple paragraphs of a string, such
AB
Cd
E
In windows, AB [CR] [LF] cd [CR] [LF] e

In windows, the carriage return and line feed in the text are stored as: 0D 0A. That is to say, "enter \ r" <CR> is stored first, and "line feed \ n" is stored again. <LF>
CR is represented by '\ R'. the ASCII code is 13 and the hexadecimal value is 0x0D;
LF is represented by '\ n'. the ASCII code is 10 and the hexadecimal value is 0x0A;

Regular Expression :(? M) ^ (\ d \ w +) (\ s *) $

Sample text

Matching result

In Expresso and PHP, enable multiline mode. "$" matches the end position of the string or the position before "\ n.

Single Row Mode

Enable single-line mode:. can match any character (including line breaks)
Close single line mode:. Only any other characters that match non-line breaks (. can match \ r, that is, all characters except not matching \ n .)

Multi-row mode affects matching of ^ and $
Single Row mode affects. Matching

The multiline mode must contain ^ or $ or both. Otherwise, even if m is added, it makes no sense.

Single-row mode and multi-row mode are two concepts that cannot be reached by a single-row mode. The concept of mutual exclusion between two MS is created only for the historical reasons of the development of regular expressions.
The single-row mode affects the matching range of the decimal point ".".
The multi-row mode affects the matching range of "^" and "$ ".

As for the following concepts, global matching does not necessarily relate to the multi-row mode and greedy mode.

Global match is disabled. Only the first successful match is matched. Global match is enabled. All successful matches are matched.
Global mode is a concept only available in some scripting languages.
When matching, disable the global mode, which is similar to the Match method in. NET. enable the global mode, similar to the Matches method in. NET.
When the replacement is performed, the global mode is disabled. Similar to the replaceFirst in Java, the global mode is enabled, similar to the replaceAll in Java.

(When matching, disable global mode, similar to the preg _ match function in PHP; enable global mode, similar to the preg _ match _ all function in PHP)

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: info-contact@alibabacloud.com 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.