Difference between Greedy Reluctant Possessive and greedypossessive in Java Regular Expression

Source: Internet
Author: User

Difference between Greedy Reluctant Possessive and greedypossessive in Java Regular Expression


In the previous article, regular expressions in programming thoughts, the principles, usage methods, and common regular expressions of regular expressions are described, this article will further explore the differences among Greedy, Reluctant, and Possessive rules in Java regular expression.

From the official Java documentation greedy, Reluctant (barely) and Possessive (exclusive ). Its meaning is as follows:

Greedy quantifiers

X?

X, neither once nor once

X *

X, zero or multiple times

X +

X, once or multiple times

X {n}

X, EXACTLY n times

X {n ,}

X, at least n times

X {n, m}

X, at least n times, but not more than m times

 

 

Reluctant quantifiers

X ??

X, neither once nor once

X *?

X, zero or multiple times

X ++?

X, once or multiple times

X {n }?

X, EXACTLY n times

X {n ,}?

X, at least n times

X {n, m }?

X, at least n times, but not more than m times

 

 

Possessive quantifiers

X? +

X, neither once nor once

X * +

X, zero or multiple times

X ++

X, once or multiple times

X {n} +

X, EXACTLY n times

X {n,} +

X, at least n times

X {n, m} +

X, at least n times, but not more than m times

 

Greedy, Reluctant, and Possessive

Looking at the above table, we found that the three quantifiers have the same meanings (such as X? , X ?? , X? + Both represent one or none), but there are some minor differences between them. Let's take an example:

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:

 

// Mismatch successful

 

Principles

Greedy quantifiers are called Greedy because the matcher is forced to read the entire input string during the first attempt. If the first attempt fails to match, then, it will be rolled back from the back to the back one by one character and try again until the match is successful or no character can be rolled back.

Mode string:. * foo

Search string: xfooxxxxxxfoo

Result: matched form 0 to 13

 

The comparison process is as follows:

 


Reluctant adopts the opposite method of Greedy. It starts from the first (character) Position of the input string and barely reads one character at a time until it attempts to match the entire string.

Mode string:. * foo

Search string: xfooxxxxxxfoo

Result: matched form 0 to 4

Matched form 4 to 13

 

The comparison process is as follows:

 

 

Possessive quantifiers always read the entire input string, and the matching is successful once (only once). Unlike Greedy, Possessive never rolls back. Even if this is done, the overall matching may be successful.

Mode string:. * foo

Search string: xfooxxxxxxfoo

Result:

// Mismatch successful

 

The comparison process is as follows:

 

 

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

 

Let's take a look at several examples:

Mode string:. + [0-9]

Search string: abcd5aabb6

Result: matched form 0 to 10

 

 

Mode string:. +? [0-9]

Search string: abcd5aabb6

Result: matched form 0 to 4

 

 

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

Search string: abcd5aabb6

Result: matched form 0 to 10

 

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

Search string: abcd5aabb6

Result: The matching fails.

 

 

If you have any questions or thoughts, please give feedback in the comments. Your feedback is the best Reviewer! Due to my limited technical skills and capabilities, if this blog post contains errors or deficiencies, please forgive me and give your valuable suggestions!

 

 

= ====================

Regular Expressions of programming ideas

Programming idea iterator

Recursion of programming ideas

Callback of programming ideology

 

 

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.