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

The difference between Greedy Reluctant Possessive in Java regular expressions

高洛峰
高洛峰Original
2016-10-15 13:36:432106browse

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