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

Detailed explanation of the three modes of regularity (greedy, reluctant and possessive)

php中世界最好的语言
Release: 2018-03-30 11:26:39
Original
2485 people have browsed it

This time I will bring you a detailed explanation of the three modes of regularity (greedy, reluctant, and possessive), and Precautions for using the three modes of regularity (greedy, reluctant, and possessive) What are they? Here are actual cases. Let’s take a look.

Greediness (Greediness): Maximum matching

X?, X*, X+, X{n,} is the maximum matching. For example, if you want to use "<.+>" to match "aaava abb", maybe the result you expect is to match "", but the actual result will match Go to "aava .

In Greediness mode, it will try to match as wide a range as possible until the entire content is matched. At this time, when it is found that the match cannot be successful, it will start to shrink back a little. Matching range until successful match

String test = "a<tr>aava </tr>abb ";
String reg = "<.+>";
System.out.println(test.replaceAll(reg, "###"));
Copy after login

Output: a

abb


Reluctant(Laziness) (reluctant): minimum match

X??, X*?, X+?, X{n,}? is the minimum match. In fact, X{n, m}? and As long as the match is successful, no more attempts will be made to match a wider range of content

String test = "a<tr>aava </tr>abb ";
String reg = "<.+?>";
System.out.println(test.replaceAll(reg, "###"));
Copy after login
Output: a

aava

abb

Unlike Greediness, content is matched twice in Reluctant mode


Possessive (possessive): exact match

X?+,

#Possessive mode has a certain similarity with Greediness, that is, it tries to match the largest range of content until the end of the content, but unlike Greediness, full matching no longer falls back and tries to match a smaller range ##. #

String test = "a<tr>aava </tr>abb ";
String reg = "<.++>";
String test2 = "<tr>";
String reg2 = "<tr>";
System.out.println(test.replaceAll(reg, "###"));
System.out.println(test2.replaceAll(reg2, "###"));
Copy after login
Output: aaava abb

I believe you have mastered the method after reading the case in this article. Please pay attention to other related articles on the php Chinese website for more exciting information!

Recommended reading:

Detailed explanation of using regular expressions in Linux

Detailed explanation of using regular expressions

The above is the detailed content of Detailed explanation of the three modes of regularity (greedy, reluctant and possessive). 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