Suppose the string to be processed is Xfooxxxxxxfoo
mode. *foo (greedy mode):
The pattern is divided into sub-mode P1 (. *) and sub-mode P2 (foo) two parts. The quantifier matching method in P1 uses the default (greedy type).
When the match starts, eat all the characters xfooxxxxxx to match the sub-pattern P1. The match succeeds, but since then there is no string to match the sub-pattern P2. This round of matching failed; second round: Reduce the number of matches in the P1 section, spit out the last character, and divide the string into XFOOXXXXXXFO and O two substrings S1 and S2. S1 matches P1, but S2 does not match P2. This round of matching failed; third round, again reduce P1 partial match amount, spit out two characters, the string is divided into Xfooxxxxxxfo and oo two parts. The result is ibid. The fourth round, again reducing the P1 match amount, the string is divided into xfooxxxxxx and foo two parts, this time s1/s2 respectively and P1/p2 match. Stop trying to return the match success.
mode. *?foo (barely mode): minimum matching mode.
The first attempt to match, p1 because it is 0 or any time, so is ignored, with a string to match P2, failed; second, read the first character X, try and P1 match, match successfully; The remainder of the string Fooxxxxxxfoo the first three characters and P2 are also matched. Therefore, stop trying to return the match success. In this mode, if the remaining string continues to look for a substring that matches the pattern, another xfoo at the end of the string is found, and in greedy mode, no second matching substring exists because the first successful substring of the match is already all characters.
mode. *+foo (Embezzlement mode): also known as occupancy mode.
The match starts when all strings are read, and the P1 matches successfully, but there are no strings left to match P2. Therefore, the match fails. Return.
Simply put, greedy mode and the mode of possession, greedy mode will only partially match the success of the condition, in order to reduce the number of matches to the successful part of the pattern of matching numbers, the characters left to the pattern of the other parts to match; The occupancy pattern is to occupy all the successful parts of the match, and never leave it to other parts.
Look at the following example: comparison between greedy mode and embezzlement mode
Regular: \w+[a-z] and \w++[a-z]
Target string: 232hjdhfd7474$
Analysis: ①\w+[a-z]:\w+ belongs to greedy mode, will eat all the characters it can eat at once, that is, substring 232hjdhfd7474, at this time [a-z] can not find a match, so \w+ matching string will spit out a character 4, but at this time still do not get a match. Repeated this spit back, until the character D is spit, this time [a-z] can match H, so the regular expression will return a successful match result, 232HJDHFD
②\w++[a-z]:\w++ belongs to the embezzlement mode, it will eat all the characters it can eat, that is, substring 232hjdhfd7474, and not left to other parts to use, it will not be rolled back. At this point [a-z] is not able to find a match, so this match failed. The substring that matched the success was not found in the remaining substrings. So the entire regular expression is unable to find the matching result!
Type of quantifier |
Significance |
Greed |
Barely |
Occupation |
X? |
X?? |
X?+ |
Match X 0 or one time |
X* |
X*? |
X*+ |
Match X 0 or more times |
X+ |
X+? |
X++ |
Match X one or more times |
X{n} |
X{n}? |
X{n}+ |
match X N Times (This should not exist in these modes, that is, a fixed match n) |
X{n,} |
X{n,}? |
X{n,}+ |
Match X at least n times |
X{n,m} |
X{n,m}? |
X{n,m}+ |
Match X at least n times, but not more than m times |
Three patterns of JAVA regular expressions: greed, reluctance, and possessive discussion