Greediness (Greediness): Maximum matching
X?, X*, X+, X{n,} is the maximum matching. For example, if you want to use "618b16d23a3ddab818f44e3be2c27e38" to match "aa34de1251f0d9fe1e645927f19a896e8aava fd273fcf5bcad3dfdad3c41bd81ad3e5abb", maybe the result you expect is to match "a34de1251f0d9fe1e645927f19a896e8", but the actual result will match Go to "a34de1251f0d9fe1e645927f19a896e8aava fd273fcf5bcad3dfdad3c41bd81ad3e5.
In Greediness mode, it will try to match as wide a range as possible until the entire content is matched. At this time, when it is found that the match cannot be successful, it will start to shrink back a little. Matching range until successful match
String test = "a<tr>aava </tr>abb "; String reg = "<.+>"; System.out.println(test.replaceAll(reg, "###"));
Output: a
abb
Reluctant(Laziness): minimum match
String test = "a<tr>aava </tr>abb "; String reg = "<.+?>"; System.out.println(test.replaceAll(reg, "###"));Output: a
aava
abbUnlike Greediness, Reluctant mode The following content is matched twice
Possessive (possessive): exact match
String test = "a<tr>aava </tr>abb "; String reg = "<.++>"; String test2 = "<tr>"; String reg2 = "<tr>"; System.out.println(test.replaceAll(reg, "###")); System.out.println(test2.replaceAll(reg2, "###"));Output: aa34de1251f0d9fe1e645927f19a896e8aava fd273fcf5bcad3dfdad3c41bd81ad3e5abbMore Java regular expression matching patterns (greedy, reluctant) , Possessive type) Please pay attention to PHP Chinese website ### for related articles!