Getting Started with regular Expressions (vii) quantifiers

Source: Internet
Author: User

1223334444555556666667777777888888889999999990000000000

Paste the numbers arranged in right triangle into the program.
Greed, laziness and possession
The quantifier itself is greedy, and the greedy quantifier will first match the entire string. When trying to match, it selects as much content as possible, which is the entire input. The quantifier first attempts to match the entire string, and if it fails, it returns to a character and then tries again, a process called backtracking. It rolls back one character at a time, knowing that a match is found or no character is attempted.
Laziness means it starts at the beginning of the target and tries to find a match, one character at a time checking the string. Finally, it tries to match the entire string. To make a quantifier lazy, you must add a greeting after the ordinary quantifier.
The possessive word covers the entire target and then tries to find the match, but it only tries once and does not backtrack. The possessive word is to add a plus sign after the ordinary quantifier.

To match with *,+ and?
In the tool, enter

.*

It will match all the characters in the text in a greedy way. * Match any character 0 or more times.
The greedy quantifier


Match a specific number of times
Use curly braces to limit the number of times a pattern matches within a range, and the unmodified quantifier is a greedy quantifier.
For example:

7{1}

Matches the first occurrence of 7, to match one or more 7, just add a comma:

7{1,}

So, 7+ in fact, and 7{1,} is the same.
Well, 7* is actually the same as 7{0,}.
And finally, 7? is actually the same as 7{0,1}.
Range syntax

Lazy quantifiers

Use in Tools? To match 0 or one of 5

5?

And one more? To program the quantifier lazy

5??

Now it doesn't match anything, because the pattern is lazy now. It does not force a match to the first 5, and the basic characteristic of laziness is to match as few characters as possible .

Try a match for 0 or more

5*?

It also does not match any content, because it chooses to match the fewest number of times, 0 times.

Try to match one or more

5+?

It matches a 5, it chooses the fewest number of times, once.

Using M and n is the same way.

5{2,5}?

It will match 2 5 instead of 5.

If you want to match the least number, not the most, use the lazy quantifier.

Share words

The possessive quantifier is like a greedy quantifier, it chooses as much content as possible, but it does not backtrack. It doesn't give up what it finds, it's selfish. The advantage is that speed is fast.

We first match multiple zeros that begin with 0, and then match multiple zeros ending with 0.

0.*+

All 0 have been marked bright.

. *+0

does not match any content. Because it did not backtrack, all of a sudden selected all the input, no longer back to view. It did not find at the end of the 0, also do not know where to find out.

Getting Started with regular Expressions (vii) quantifiers

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.