: This article mainly introduces my personal understanding of regular expressions-lazy matching. For more information about PHP tutorials, see. Problem Description
Link: http://www.hcoding.com /? P = 130
When you are new to regular expressions, you have a question. for example, you need to match the character between the first pair of "_" in the string "_ abc_123, when I first learned the regular expression, I wrote "/_ \ w * _/". the matching result is "abc_123" instead of "abc". The question mark is added, "/_ \ w *? _/". At this time, the matching result is" abc ".
We know '? 'When used separately, it indicates that the repeat is zero or once, and when '? 'The occurrence following the repeated delimiters serves as a lazy match, that is, matching as few characters as possible. Lazy qualifier description:
- *? : Repeat any time, but as few as possible
- +? : Repeat once or more times, but as few as possible
- ?? : Repeated 0 or 1 times, but as few as possible
- {N, m }? : Repeat n to m times, but as few as possible
- {N ,}? : Repeated more than n times, but as few as possible
Right, "as few duplicates as possible", which is a rough and straightforward explanation of lazy matching.
So how can we get "as few duplicates as possible? We can explain this by ignoring the quantifiers in the regular expression.
Ignore quantifiers
Quantifiers "*? "," +? ","?? "," {N, m }? "," {N ,}? "All are ignored priority quantifiers. Which of the following is used to ignore priority quantifiers? , +, *, {} Are added after? In the format. For example, 'AB ?? If 'Abb 'is matched, 'A' instead of "AB" is obtained ". After the engine matches a successfully, the engine first chooses not to match B because it ignores the priority. then, it checks the expression and finds that the expression is complete. then, the engine directly reports that the matching is successful. For more information, see the following example.
Example
Or in the above example, use "/_ \ w *? _/"Matches the character between the first pair" _ "in" _ abc_123.
After the first '_' is matched, '\ w *? 'First, it is determined that no character needs to be matched, because it ignores the priority quantifiers, and then the expression'/_ \ w *? _/'In the second' _ '(' \ w *? '_' After '_') matches 'A' in '_ abc_123 _' of the target string. if the matching fails, '\ w * is used *? 'Try unmatched branches (use \ w to match a, and try to match a successfully)
In the next step, do you want to try matching or ignore it? Because '\ w *? 'Indicates that the priority quantifiers are ignored. If yes, the previous step is repeated.' _ 'fails to match B.' \ w *? 'Try the unmatched branch AB. after the above steps are repeated three times in total (until the expression '\ w *? '_' After '_' matches the second '_' of the target string.) then, 'ABC' is matched '.
Process (after matching the first ):
- Expression/_ \ w *? The second '_' in _/'matches 'A' in' _ abc_123 _ 'of the target string. The match fails.' \ w *? 'A' in the '_ abc_123 _' target string to be matched successfully.
- Expression/_ \ w *? The second '_' in _/'matches' B 'in' _ abc_123 _ 'of the target string. the matching fails.' \ w *? 'AB' in '_ abc_123 _' of 'attempt to match the target string. The match is successful.
- Expression/_ \ w *? The second '_' in _/'matches 'C' in' _ abc_123 _ 'of the target string. the matching fails.' \ w *? 'ABC' in the '_ abc_123 _' target string to be matched successfully.
- Expression/_ \ w *? The second '_' in _/'matches' _ 'in' _ abc_123 _ 'of the target string. The match is successful and the match ends. The result is abc.
The above is the idea of reading the section "proficient in regular expressions" about ignoring quantifiers. if you are not willing to accept your suggestions, thank you!
Link: http://www.hcoding.com /? P = 130
Original article. For more information, see JC & hcoding.com.
The above introduces my personal understanding of regular expressions-lazy matching, including some content, and hope to be helpful to friends who are interested in PHP tutorials.