The difference between greedy reluctant possessive in Java regular expression

Source: Internet
Author: User

the difference between greedy reluctant possessive in Java regular expressionCategory: Java2015-01-16 00:28 1280 people read Comments (9) favorite reports Regular expression Java

Directory (?) [+]

In the previous article, "The regular expression of the idea of programming," the principle of regular expression, the use of methods and the common regular expression summary, this article will further explore the Java regular expression in greedy, reluctant, possessive three different strategies.

Official documentation from Java http://docs.oracle.com/javase/7/docs/api/java/util/regex/ As we can see in the pattern.html, the regular expression represents three sets of symbols for the number of words, namely greedy (greedy), reluctant (grudging), and possessive (exclusive). The implication is as follows:

Greedy number of words

X?

X, not once or once

x*

X, 0 or more times

x+

X, one or more 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 number of words

X??

X, not once or once

X*?

X, 0 or more times

X+?

X, one or more 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 number of words

x?+

X, not once or once

x*+

X, 0 or more times

X + +

X, one or more 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, possessive the difference instance talk

Looking at the table above, we found that the meanings of these three quantitative words are the same (such as x, X,??、 x?+ all mean one or one time), but there are some subtle differences between them. Let's first look at an example:

1.Greedy

[Java]View Plaincopy
  1. Public static void Testgreedy () {
  2. Pattern p = pattern.compile (". *foo");
  3. String StrText = "Xfooxxxxxxfoo";
  4. Matcher m = P.matcher (StrText);
  5. While (M.find ()) {
  6. System.out.println ("matched form" + m.start () + "to" + m.end ());
  7. }
  8. }
Results:

Matched form 0 to 13

2.Reluctant

[Java]View Plaincopy
  1. Public static void Testreluctant () {
  2. Pattern p = pattern.compile (". *?foo");
  3. String StrText = "Xfooxxxxxxfoo";
  4. Matcher m = P.matcher (StrText);
  5. While (M.find ()) {
  6. System.out.println ("matched form" + m.start () + "to" + m.end ());
  7. }
  8. }
Results:

Matched form 0 to 4

Matched Form 4 to 13

3.Possessive

[Java]View Plaincopy
  1. Public static void Testpossessive () {
  2. Pattern p = pattern.compile (". *+foo");
  3. String StrText = "Xfooxxxxxxfoo";
  4. Matcher m = P.matcher (StrText);
  5. While (M.find ()) {
  6. System.out.println ("matched form" + m.start () + "to" + m.end ());
  7. }
  8. }
Results:

No match succeeded

Principle explanation

The greedy number word is called "greedy" because the match is forced to read into the entire input string the first time the match is attempted, and if the first attempt to match fails, then the word Fu backwards from backward and tries to match again until the match succeeds or no characters can be rolled back.

Pattern string:. *foo

Find string: Xfooxxxxxxfoo

Results: Matched form 0 to 13

The comparison process is as follows


Reluctant uses the opposite approach to greedy, which starts with the first (character) position of the input string, and only reluctantly reads one character in an attempt to match the search until the entire string is tried out.

Pattern string:. *foo

Find string: Xfooxxxxxxfoo

Results: Matched form 0 to 4

Matched Form 4 to 13

The comparison process is as follows

Possessive the number of words is always read into the entire input string, try once (and only once) match successfully, unlike greedy,possessive never fallback, even if this can make the overall match success.

Pattern string:. *foo

Find string: Xfooxxxxxxfoo

Results:

No match succeeded

The comparison process is as follows

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

Take a look at a few more examples:

Mode string:. +[0-9]

Find string: Abcd5aabb6

Results: Matched form 0 to 10

Mode string:. +? [0-9]

Find string: Abcd5aabb6

Results: Matched form 0 to 4

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

Find string: Abcd5aabb6

Results: Matched form 0 to 10

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

Find string: Abcd5aabb6

Result: Match failed

If you have any doubts and ideas, please give feedback in the comments, your feedback is the best evaluator! Due to my limited skills and skills, if the Ben Boven have errors or shortcomings, please understand and give your valuable advice!

======================== Welcome to the series of articles on programming ideas ========================

The regular expression of programming thought

An iterative device for programming ideas

Recursion of programming thought

The callback of programming thought

The difference between greedy reluctant possessive in Java regular expression

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.