Regular expression lazy-match pattern (?)

Source: Internet
Author: User
Regular expression lazy-match pattern:
In the greedy matching model chapter has said that human nature is greedy, hoping to get more money, status and even beauty, but there are a lot of ascetic people, as long as the basic needs of life can be met, in the regular expression also has such a matching principle, the following is introduced.

I. The concept of lazy mode:

This pattern is exactly the opposite of greedy mode, which matches as few characters as possible to satisfy regular expressions, for example:

var str= "axxyyzbdkb"; Console.log (Str.match (/a.*b/));

The above code is greedy mode, so it is able to match the entire string, and the following modifies it into an inert matching pattern:

var str= "axxyyzbdkb"; Console.log (Str.match (/a.*?b/));

The code above is a lazy match, by adding a question mark (?) after the repetition quantifier. Can.
The lazy match pattern is as few matching characters as possible, but must satisfy the regular expression's matching rules, such as the above code, which can be repeated to match 0 or more preceding characters or sub-expressions, but the regular expression must end with B, so the regular expression can match the Axxyyzb in the string above.

Summarized as follows:

1. Add a question mark (?) after a repeating quantifier. Can form an inert match.
2. The lazy match will match the characters as few as possible, but the entire matching pattern must be met.

Two. List of lazy qualifiers:

The following is my profile

In fact, greed and inertia is easy to understand, from the literal meaning we can know that the so-called "greed" means that if the requirements are matched until the match, until the match, this is the greedy mode. The so-called inertia mode is once the match to the appropriate end, do not continue to match down, I would like to introduce a few examples to explain the main.

First, tell me about the greedy pattern identifier: +,? , *,{n},{n,},{n,m}. Lazy mode: +? ,?? ,*?? , {n}?,{n,}?,{n,m}?;

Example One

var pattern=/8[a-za-z0-9]*7/; greedy mode var string= "Abc8defghij7klngon8qrstwxy7";

At this time using the greedy mode *, indicating that there can be any number of letters between 8 and 8, then this is the first match of 8, if the match to the following, the unlimited match after the content, as long as the contents of the following meet [a-za-z0-9] can. Always match, match to no longer match, see immediately after the next one is not 7, if not that he went forward into one (spit out a look is not 7), if not continue to spit until Spit 7, and then match to the content between this. So the result matches the whole string.

var pattern=/8[a-za-z0-9]*?7/ig; lazy mode var string= "Abc8defghij7klngon8qrstwxy7";

The above is using the lazy mode *?, the matching method is this, first match a 8, and then match a character in the next to see if it is not in line with [a-za-z0-9], if the match, then go to see immediately after the next character is not 7, if it is 7 on the end, if not the next match a character, See whether it is in line with [a-za-z0-9], if the match, then see immediately after the next character is not 7, if it is 7 on the end, otherwise, according to the above way loop down, the guidance to meet so far.

(2). Greed and inertia patterns can also be expressed in another way.

Example Two

var test= "]*\/>/ig;

This can also be implemented in the lazy mode, [^>] This means that the can not appear between, so the result is to find each tag.

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.