java regular expression - quantifier

Regular Expressions - quantifier

1 Overview

The so-called quantifiers, can be understood as used to describe the number of times a character (character set) recurring. For example, 11 -digit phone number, without considering the top three fixed combination, with a regular expression written as:

\d\d\d\d\d\d\d\d\d\d\d

Where d is repeated 11 times, very inconvenient, so we introduced a quantifier, the formula can easily be written as:

\d{11}

Classifiers description also supports a length range, the form {m, n} m and n are two integers, the mathematical equivalent of [m, n] , for example, \ D {1,11} , represents the numeric character string length of the shortest . 1 characters maximum . 11 characters.

 

[Note] {m, n} is n will be omitted written {m,} represents the least characters appear m times, and there is no upper limit.

2 common quantifiers representation

There are several techniques commonly used by the quantifier, he said quantifier produce the equivalent of shorthand strategy.

 

3 dot

Meta special characters . (Dot), except that dot can match newline n all characters outside, including numbers, letters and other symbols. If a real need to match all characters, the dot may be used in the case where single-line pattern matching, using the first chapter or talk, using [\ S \ S] .

 

4 matching quantifiers

Regular expression quantifiers can be divided into several categories, before the introduction of quantifiers can all be classified as greedy quantifier, it matches the policy is: When in doubt whether or not to match, the first attempt to match, do the follow-up action.

Because there are greedy exist for wildcards (except \ the n- ) point number is likely to match should not have to match the part (such as end of the string), this time we should give up this match, so the expression part try to match. This is a process known as backtracking.

 

5 lazy quantifier

Instead, choose not to match known as lazy quantifiers to match the time when uncertainty. Explore the use of partial matches behind expressions do not meet the backtracking operation.

Quantifier mentioned above are commonly used in matching priority, they are lazy corresponding quantifiers, as follows:

 

[Note] to escape lazy quantifiers include front and rear two parts, namely *? Escape is \ * \? +? Is escaped as \ + \?, ?? escaped as \? \?

Like this article can follow me, I will continue to update, update your concerns are my motivation! For more java learning materials may also private letter I!

Guess you like

Origin www.cnblogs.com/heqingxiaohuo/p/12180735.html