search
Home类库下载java类库The difference between Greedy Reluctant Possessive in Java regular expressions

From Java’s official documentationhttp://docs.Oracle.com/javase/7/docs/api/java/util/regex/Pattern.html we can see that there are three sets of symbols for regular expressions to represent quantifiers. They are Greedy (greedy), Reluctant (reluctant) and Possessive (exclusive). The meaning is as follows:

The difference between Greedy Reluctant Possessive in Java regular expressions

The difference between Greedy, Reluctant and Possessive

Examples

Looking at the table above, we find that the meanings of these three quantifiers are the same (such as X?, X??, X?+ all means once or not once), but there are still some subtle differences between them. Let’s look at an example first:

1.Greedy

public static void testGreedy() {  
    Pattern p = Pattern.compile(".*foo");  
    String strText = "xfooxxxxxxfoo";  
    Matcher m = p.matcher(strText);  
    while (m.find()) {  
        System.out.println("matched form " + m.start() + " to " + m.end());  
    }  
}

Result:

matched form 0 to 13

2.Reluctant

public static void testReluctant() {  
    Pattern p = Pattern.compile(".*?foo");  
    String strText = "xfooxxxxxxfoo";  
    Matcher m = p.matcher(strText);  
    while (m.find()) {  
        System.out.println("matched form " + m.start() + " to " + m.end());  
    }  
}

Result:

matched form 0 to 4

matched form 4 to 13

3.Possessive

public static void testPossessive() {  
    Pattern p = Pattern.compile(".*+foo");  
    String strText = "xfooxxxxxxfoo";  
    Matcher m = p.matcher(strText);  
    while (m.find()) {  
        System.out.println("matched form " + m.start() + " to " + m.end());  
    }  
}

Result:

//Unmatched successfully

Principle explanation

Greedy quantifier is called "greedy" because the matcher is forced to read the entire input string when trying to match for the first time. If If the first attempt to match fails, the character will be rolled back character by character from back to front and the match will be attempted again until the match is successful or there are no characters to fall back on.

Pattern string: .*foo

Search for string: ) position, barely reading one character at a time until the entire string is tried.

Pattern string:.*foo

Search string: , attempts a successful match once (and only once), unlike Greedy, Possessive never rolls back, and even doing so may make the overall match successful.

Pattern string: .*fooThe difference between Greedy Reluctant Possessive in Java regular expressions

Search string: xfooxxxxxxfoo

Result:

//Unmatched successfully

The comparison process is as follows

Reference article: http://docs.oracle.com/javase /tutorial/essential/regex/quant.html

Let’s look at a few more examples: The difference between Greedy Reluctant Possessive in Java regular expressions

Pattern string: .+[0-9]

Search string: abcd5aabb6

Result: matched form 0 to 10

Pattern string: .+?[0-9]

Search string: abcd5aabb6

Result: matched form 0 to 4

The difference between Greedy Reluctant Possessive in Java regular expressions

Pattern string: .{1,9}+[0-9]

Search string: abcd5aabb6

Result: matched form 0 to 10

Pattern string: .{1,10}+[0-9]

Search string: abcd5aabb6

Result: Match failed


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool