A RegEx greedy and non-Greedy question

Source: Internet
Author: User
{Code...} Add a question mark (+?) after the plus sign (+ ?) It is to cancel the default greedy pattern match. According to the regular expression above, the expected output is 1_4. Why is the first group d +? Wrong match, while the second group d +? Match to a 4. Do not understand?
$ Exp = '/\ d +? \_\ D +? /'; $ Str = "My phone number is 123_45678"; preg_match ($ exp, $ str, $ match); echo ($ match [0]); // '2017 _ 4'

Add a question mark (I .e.,(+?)It is to cancel the default greedy pattern match. According to the regular expression above, the expected output is1_4Yes. Why Group 1\d+?Incorrect match, whereas the second group\d+?Match to a 4. Do not understand?

Reply content:
$ Exp = '/\ d +? \_\ D +? /'; $ Str = "My phone number is 123_45678"; preg_match ($ exp, $ str, $ match); echo ($ match [0]); // '2017 _ 4'

Add a question mark (I .e.,(+?)It is to cancel the default greedy pattern match. According to the regular expression above, the expected output is1_4Yes. Why Group 1\d+?Incorrect match, whereas the second group\d+?Match to a 4. Do not understand?

The mechanism of regular expressions is finite state automation. It matches the regular expression from the first character of a string.

So,\d+?_\d+?This regular expression starts from the first character of the string (that is, '''my'''). If it finds that it does not meet the condition, it then matches it until it is matched.1This character is found to match\d+?, Save, continue matching, match_This character is already in line\d+?_All the conditions of4Because it is a non-Greedy mode4It will end. If it is greedy, it will match to the last 8.

In the above process\d+?Yesfrom1Start matching and record, because there are_So it will123Only in this way can the regular expression be satisfied. This time is not greedy.

For non-Greedy users, the following example may help you better understand it.

There is such a string

1

If <(.*?)>Will match

And

In greedy mode, the entire string is matched.
The difference is that (.*)When it is not greedy, find the First Regular Expression >And the matching is completed. When greedy, regular expressions need to find the final matching condition. >To stop matching. Of course, this is also related. If it is changed to \ w +, it will not be affected if it is greedy or greedy.
I may not be very clear about it. You can try again.

'/\d+?\_\d+?/'

A string of numbers (at least one) as few as possible), Followed by_, FollowedA string of numbers (at least one) as few as possible)

Let's take a look at this whole and connect it with the intermediate words...

Or, in other words, the regular expression matches consecutive character sequences, and the split is considered as a ghost...

GreedyAndNon-Greedy (inert) Both must be matched successfully.The difference is:

GreedyIf the matching is successful,Match as many as possible
Non-Greedy (inert)If the matching is successful,Match as few as possible

At the same time, the regular expression has another ruleGreedy,Non-Greedy (inert)The rule has a higher priority.

The first match has The highest priority -- The match that begins earliest wins

Based on the above principles, return to the regular expression of the landlord:
First\d+?Because there is_, It will match123_(High priority) instead3_It's not what the landlord said.1_(The matching fails );
Second\d+?Will match4(Match as few as possible) instead45678

Contact Us

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.