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 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: .*foo
Search string: xfooxxxxxxfooResult: //Unmatched successfullyThe comparison process is as followsReference article: http://docs.oracle.com/javase /tutorial/essential/regex/quant.html
Let’s look at a few more examples:
Pattern string: .+[0-9]Search string: abcd5aabb6Result: matched form 0 to 10 Pattern string: .+?[0-9]Search string: abcd5aabb6Result: matched form 0 to 4Pattern 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