PHP extension-text processing-PCRE regular expression syntax 9-repeated quantifiers are specified by quantifiers and can be followed by the following elements:
Individual characters, which can be escaped
Metacharacters.
Character class
Back reference (join the next part)
Sub-group (unless it is an asserted)
A general repeated quantizer specifies the number of times a minimum value matches a maximum value. two numbers are enclosed in curly brackets, and the two numbers are defined using the syntax separated by commas. Both values must be less than 65536, and the first number must be less than or equal to the second. For example, z {2, 4} matches "zz", "zzz", and "zzzz ". A single right curly braces are not special characters. If the second digit is omitted, but the comma still exists, there is no upper limit. if the second digit and the comma are ignored, the quantifiers are limited to a matching of the specified number of times. For example, [aeiou] {3,} matches at least three consecutive vowels, but can also match more, while \ d {8} can only match eight numbers. The left curly braces are considered to be a common character when they are not allowed to use quantifiers or do not match the quantifiers syntax. For example, {, 6} is not a quantizer and will match four characters "{, 6}" according to the original text }".
The quantifiers {0} are authorized, and the behavior is that the previous item and quantifiers do not exist.
For convenience (and historical compatibility), the three most commonly used quantifiers have single-character abbreviations.
Single-character quantifiers
* Equivalent to {0 ,}
+ Equivalent to {1 ,}
? Equivalent to {0, 1}
A subpattern that does not match any character followed by a 0 or multiple character quantifiers can be used to construct an infinite loop without an upper limit. For example: (?) *
Earlier versions of perl and pcre may get an error during compilation. However, because this is useful in some cases, we now accept this pattern. However, if the repetition of any sub-pattern does not match any characters, the loop will be forced out.
By default, quantifiers are "greedy", that is, they will match as many characters as possible (until the maximum number of matching times is allowed) without causing a pattern match failure ). A typical example of this problem is to try to match the C language annotations. All content that appears between/* and */is considered as comments. in the middle of comments, separate * and/may appear /. One attempt to match the C annotation is to use the mode /\*. * \ */. if this mode is applied to the string "/* first comment */not comment/* second comment */", it will match the wrong result, that is, the entire string, which is caused by the greedy nature of quantifiers. it will try to match as many characters as possible.
However, if a quantizer is followed by one? (Question mark) mark, it will become a lazy (non-greedy) pattern, it no longer matches as much as possible, but as few as possible. So the mode /\*.*? \ */The statement will be correctly executed in C's annotation matching. The meaning of each quantizer is not changed, but is it added? Change the number of matching times. Do not set? This usage is confused with its usage as a quantizer. Because of its two usage methods, sometimes it may contain quantifiers, such as \ d ?? \ D is more inclined to match a number, but it can also accept matching of two numbers to achieve the purpose of matching the entire pattern. : In mode \ w \ d ?? \ D \ w for example, for the string "a33a", although \ d ?? It is not greedy. However, if greedy is used, the entire pattern does not match. Therefore, the final choice is still matching a number.
If the PCRE_UNGREEDY option is set (an option that is unavailable in perl), the quantifiers are not greedy by default. However, a single quantizer can follow one? To make it greedy. In other words, the PCRE_UNGREEDY option reverses greedy default behavior.
Followed by a "+" is "possession. It will eat as many characters as possible and will not focus on other subsequent modes, such. * abc matches "aabc",. * + abc does not match because. * + will eat the entire string, resulting in the remaining pattern not matching. Since PHP 4.3.3, you can use the placeholder (+) modifier to increase the speed.
When a sub-group is modified by a quantizer whose minimum quantity is greater than 1 or which has a maximum quantity limit, more storage is required for the compilation mode according to the minimum or maximum quantity ratio.
If a mode uses. * OR. {0,} starts and the PCRE_DOTALL option is enabled (equivalent to/s of perl), that is, allow. match the line break, then the mode will be implicitly tightened, because no matter what, next we will try every character position in the target string, so after the first time, there is no retry point for all matches at any location. PCRE will try to process this mode like \. When we know that the target string does not contain a line break, it is worth setting PCRE_DOTALL to get this optimization when the mode starts with. *, or we choose to use ^ to explicitly specify the anchor.
Note: if the mode does not match, the next position is not displayed. for example, if PCRE_DOTALL is not set and the first character of the target string is a line break, the first character is used in the mode, if no match is found, the system tries again from the second character position. After PCRE_DOTALL is used, it must match .... Similarly, if ^ or/A is used, once the mode does not match, it can exit directly, instead of starting the matching of the entire mode again at the next position of the target string.
When a sub-group is captured repeatedly, the result of the sub-group captured is the value captured in the last iteration. For example, (tweedle [dume] {3} \ s *) + matching string "tweedledum tweedledee", the obtained sub-group capture result is "tweedledee ". However, if it is a nested capture sub-group, the corresponding capture value may be set to the previous iteration. For example, if/(a | (B) +/matches the string "aba", the result of the second capture sub-group is "B ". Note: I do not understand the latter part. for example, B is the result of the last capture of the second sub-group. Therefore, the final result of the second sub-group is B, this is in line with the rules described earlier.