Personal understanding Regular expression--lazy match

Source: Internet
Author: User

Problem description

This article link: http://www.hcoding.com/?p=130

Beginner regular expression When there is a question, for example: Need to match the string "_abc_123_" in the first pair of "_" characters between the beginning to learn the regular expression will be written as "/_\w*_/", the result of matching is "abc_ 123"instead of"ABC", the big God said add a question mark,"/_\w*?_/", this time the match result is"ABC".

We know '? ' When used alone means: repeat 0 or one time, and when '? ' appearing after the repetition qualifier, the function is lazy match, that is, match as few characters as possible. Lazy Qualifier Description:

    • *: Repeat any number of times, but repeat as little as possible
    • +?: Repeat 1 or more times, but repeat as little as possible
    • ?? : Repeat 0 or 1 times, but repeat as little as possible
    • {n,m}?: Repeats N to M times, but repeats as little as possible
    • {N,}?: Repeat more than n times, but repeat as little as possible

Yes, "repeat as little as possible," which is a blunt commentary on the lazy match.

So how do you understand "repeat as little as possible"? We can explain this by ignoring the precedence quantifier from the regular expression.

Ignore precedence quantifiers

Quantifier "*?", "+?", "??", "{n,m}?", "{N,}?" All belong to ignoring the priority quantifier, ignoring the priority quantifier is used in the?, +, *, {} After the composition, ignoring the priority at the time of the match will first try to ignore, if the failure after the backtracking will choose to try. Like ' ab?? ' Match ' ABB ' will get ' a ' instead of ' ab '. When the engine matches success A, because it is ignored first, the engine chooses not to match B, continues to view the expression, and discovers that the expression is finished, then the engine directly reports the match success. Specifically, we use the following example step by step to explain the principle of ignoring the priority quantifier.

Example

Or the above example, use "/_\w*?_/" to match the characters between the first pair of "_" in "_abc_123_".

After starting to match the first ' _ ', ' \w*? ' The first decision does not need to match any characters, because it is to ignore the priority quantifiers, then take the expression '/_\w*? _/' The second '_' (' \w*? ') The following ' _ ') matches the ' a ' in the target string ' _abc_123_ ', the match fails, and then it takes ' \w*? ' To try an unmatched branch (use \w to match a, try to match a success)

The next step is to try to match or ignore it? Because ' \w*? ' is to ignore the priority quantifier, will choose to ignore, then is to repeat the previous step, ' _ ' Match B failed, ' \w*? ' To try an unmatched branch AB, the above steps have been repeated 3 times (until the expression ' \w*? '). Followed by ' _ ' and the target string the second ' _ ' match), eventually matching the ' abc '.

Process (after starting to match the first ' _ '):

  • An expression/_\w*? _/' In the second '_' and the target string ' _abc_123_ ' in the ' a ' match, the match failed, ' \w*? ' Attempt to match ' a ' in target string ' _abc_123_ ', match succeeded.
  • An expression/_\w*? _/' In the second '_' and the target string ' _abc_123_ ' in the ' B ' match, the match failed, ' \w*? ' Try to match the target string ' _abc_123_ ' in ' AB ' and match the success.
  • An expression/_\w*? _/' In the second '_' and the target string ' _abC_123_ ' in the ' C ' match, the match failed, ' \w*? ' Try to match the ' ABC ' in the target string ' _ABC_123_ ' to match the success.
  • An expression/_\w*? _/' In the second '_' and the target string ' _abc_123_ ' In the ' _ ' match, the match succeeds, the match ends. The result is ABC.

The above is to read the "proficient regular expression" on the idea of ignoring the priority quantifiers section, if there is no open-minded to accept the advice of you, thank you!

This article link: http://www.hcoding.com/?p=130

Original articles, reproduced please specify: jc&hcoding.com

Personal understanding Regular expression--lazy match

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.