# My understanding of regular expressions-lazy match, regular expression match _ PHP Tutorial

Source: Internet
Author: User
I personally understand regular expressions-lazy matching and regular expression matching. I personally understand regular expressions-lazy matching, regular expression matching problem description link: www.hcoding.com? When I was a beginner in the p130 regular expressions, I had a question. for example, I personally understand the regular expressions-lazy matching and regular expression matching.
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 *?_/'Second'_'(' \ W *? '_') And target string '_A'A' in bc_123 _ 'matches and the matching fails.' \ w * is used only when the matching fails *? '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 *?_/'Second'_'And target string '_A'A' in bc_123 _ 'matches, and the matching fails.' \ w *? 'Try to match the target string '_A'A' in bc_123 _ '. The match is successful.
• Expression/_ \ w *?_/'Second'_'And target string' _B'B' in c_123 _ 'Matches. matching fails.' \ w *? 'Try to match the target string '_AB'AB' in c_123 _ '. The match is successful.
• Expression/_ \ w *?_/'Second'_'And target string' _ ABC_ 'C' in 123 _ 'matches, and the matching fails.' \ w *? 'Try to match the target string '_Abc'ABC' in _ 123 _ '. The match is successful.
• Expression/_ \ w *?_/'Second'_'And target string' _ abc_The '_' in 123 _ 'matches, 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

Problem Description link: http://www.hcoding.com /? P = 130 I have a question when I am a beginner in regular expressions, for example...

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.