Greedy quantifier:
Let's first see if the entire string matches. If no match is found, it removes the last character in the last string and tries again. If no match is found, then remove the last string again, and the process will be repeated until a match is found or the string has no characters left. Simple quantifiers are all greedy quantifiers.
Lazy quantifier:
Let’s first see if the first letter in the string matches. If one character is not enough, read the next character to form a two-character string. If no match is found, the lazy quantifier continues to add characters from the string until a match is found or the entire string has been checked and there is no match. Lazy quantifiers work exactly the opposite way.
Dominant quantifiers:
Try to match the entire string only. If the entire string does not produce a match, no further attempt is made.
Greedy quantifiers Lazy quantifiers dominate quantifier description
-------------------------------------------------- -----------------------------------
? ?? ?+ can appear 0 or 1 time, but at most 1 time
* *? *+ can appear any time or not
+ +? ++ appears once or more times, but at least once
{n} {n}? {n}+ must appear n times
{n,m} {n,m}? {n,m}+ appears at least n times, but cannot exceed m times at most
{n,} {n,}? {n,}+ can occur any time, but at least n times. For example: we want to get a match between abbb, aabbb, aaabbb from the string abbbaabbbaabbb1234.
1. Greedy Measurement Words
The code copy is as follows:
var regexp = /.*bbb/g;
var a = str.match(regexp);
alert(a.length); //output:1
alert(a[0]); //output:abbbaabbbaaabbb
The working process of greedy quantifiers can be expressed in this way:
a) abbbaabbbaabbb1234
b)abbaabbbaabbb123
c)abbbabbaabbaaabbb12
d)abbbabbaabbaaabbb1
e)abbbabbaabbaaabbb //true
You can see that the greedy quantifier will stop working after obtaining a match, although we added 'g' (global match)
2. Lazy Measurement Words
The code copy is as follows:
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 in this way:
a)a
b)ab
c)abb
d)abbb //Save the result and start again from the next location
e)a
f)aa
g)aab
h)aabb
j)aabbb //Save the result and start over from the next location
e)a
e)aa
e)aaa
e)aaab
e)aaabb
e)aaabbb //Save the result and start over from the next position. Since JS does not support dominant quantifiers, we can only use JAVA to demonstrate dominant quantifiers:
The code copy is as follows:
String string = "abbbaabbbaaabbb1234";
Pattern p = Pattern.compile(".*+bbb");
Matcher m = p.matcher(string);
System.out.println(m.find()); //output:false
Because dominant quantifiers adopt a one-size-fits-all matching method, such as:
a) abbbabbaabbaaabbb1234 //false
The above is the entire content of this article. I hope you like it and it will be helpful for you to master Java regular matching.
Please take some time to share the article with your friends or leave a comment. We will sincerely thank you for your support!