Java regular greedy matching lazy matching

  • 2020-04-01 03:48:07
  • OfStack

Greedy quantifier:

Let's see if the whole string is a match. If no match is found, it removes the last character in the last string and tries again. If still no match is found, then       Remove the last string again, and the process repeats until a match is found or there are no characters left in the string. Simple quantifiers are all greedy quantifiers.

Lazy quantifier:

See if the first letter in the string is a match, and if one character alone is not enough, read in the next character, forming a two-character string. If no match has been found, the lazy quantifier continues to add characters from the string until a match is found or the entire string is checked and no match is found. Lazy quantifiers and greedy quantifiers work in the opposite way.

Dominant quantifier:

Just try to match the entire string. If the entire string fails to produce a match, no further attempts are made.

      Greed quantifier     Lazy quantifier       Ruling quantifier                                           describe
      -------------------------------------------------------------------------------------
          The & # 63;                         The & # 63; The & # 63;                         The & # 63; +                                           It can occur 0 times or 1 time, but at most 1 time
          *                         * & # 63;                       * +                                           It can happen any number of times, or it can't happen at all
          +                         + & # 63;                       + +                                           One or more times, but at least one time
          {n}               {n} & # 63;                     {n} +                                     It must happen n times
          {n, m}       {n, m} & # 63;             {n, m} +                             At least n times, but not more than m times
          {n,}             {n,} the & # 63;                   {n,} +                                 It could happen any number of times, but it could happen at least n times
      For example, we want to get the matches of abbb, aabbb, and aaabbb from the string abbbaabbbaaabbb1234

      1. Greedy quantifier


       var regexp = /.*bbb/g;
        var a = str.match(regexp);
        alert(a.length);   //output:1
        alert(a[0]);       //output:abbbaabbbaaabbb

      The working process of the greedy quantifier can be expressed as follows:
          A) abbbaabbbaaabbb1234
          B) abbbaabbbaaabbb123
          C) abbbaabbbaaabbb12
          D) abbbaabbbaaabbb1
          E) abbbaabbbaaabbb / / true
      As you can see, the greedy quantifier stops working after it gets a match, although we add 'g'(global match)

      2. Lazy quantifiers


        var regexp = /.*?bbb/g;
        var a = str.match(regexp);
        alert(a.length);   //output:3
        alert(a[0]);       //output:abbb
        alert(a[1]);       //output:aabbb
        alert(a[2]);       //output:aaabbb

      The working process of lazy quantifiers can be expressed as follows:
          A) a
          B) ab
          C) abb
          D)abbb // save the result and start again from the next position
 
          E) a
          F) aa
          G) aab
          H) aabb
          J)aabbb // save the result and start again from the next position
 
          E) a
          E) aa
          E) aaa
          E) aaab
          E) aaabb
          E) aaabbb  // save the result and start again from the next location
      Since JS does not support dominance quantifiers, we can only use JAVA to demonstrate the dominance quantifiers:


        String string = "abbbaabbbaaabbb1234";
        Pattern p = Pattern.compile(".*+bbb");
        Matcher m = p.matcher(string);
        System.out.println(m.find());   //output:false

      Because the dominant quantifier adopts a one-size-fits-all matching method, such as:
      A) abbbaabbbaaabbb1234 / / false

All of the above is the content of this article, hope you can enjoy, can help you master Java regular matching.

Please take a moment to share the article with your friends or leave a comment. We sincerely appreciate your support!


Related articles: