Greediness (Greediness): Maximum matching
X?, X*, X+, X{n,} is the maximum matching. For example, if you want to use "<.+>" to match "aaava abb", maybe the result you expect is to match "", but the actual result will match Go to "aava .
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
1 2 3 | String test = "a<tr>aava </tr>abb " ;
String reg = "<.+>" ;
System.out.println(test.replaceAll(reg, "###" ));
|
Copy after login
Output: a
abb
Reluctant(Laziness): minimum match
X??,X *?, In Reluctant mode, as long as the match is successful, it will no longer try to match a larger range of content
1 2 3 | String test = "a<tr>aava </tr>abb " ;
String reg = "<.+?>" ;
System.out.println(test.replaceAll(reg, "###" ));
|
Copy after login
Output: a
aava
abb
Unlike Greediness, Reluctant mode The following content is matched twice
Possessive (possessive): exact match
X?+, X*+, X++,
Possessive mode has a certain similarity with Greediness, that is, it tries to match the largest range of content until the end of the content, but unlike Greediness, exact matching no longer falls back and tries to match more. Small range.
1 2 3 4 5 6 | 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, "###" ));
|
Copy after login
Output: aaava abb
More Java regular expression matching patterns (greedy, reluctant) , Possessive type) Please pay attention to PHP Chinese website ### for related articles!