Greedy quantifier | Lazy quantifier | Description |
---|---|---|
* | *? | Match zero or more times. |
+ | +? | Match one or more times. |
? | ?? | Match zero or one time. |
{ n} | { n}? | Match exactly n times. |
{ n,} | { n,}? | Match at least n times. |
{ n, m} | { n, m}? | Match from n to m times. |
n
and m
are integer constants. Ordinarily, quantifiers are greedy; they cause the regular expression engine to match as many occurrences of particular patterns as possible. Appending the ?
character to a quantifier makes it lazy; it causes the regular expression engine to match as few occurrences as possible. For a complete description of the difference between greedy and lazy quantifiers, see the section Greedy and Lazy Quantifiers later in this topic.(a*)*
does) can increase the number of comparisons that the regular expression engine must perform, as an exponential function of the number of characters in the input string. For more information about this behavior and its workarounds, see Backtracking.*
in a regular expression pattern is interpreted as a literal asterisk ('*') character.*
quantifier matches the preceding element zero or more times. It is equivalent to the {0,}
quantifier. *
is a greedy quantifier whose lazy equivalent is *?
.95
, 929
, 9219
, and 9919
) do not.Pattern | Description |
---|---|
b | Start at a word boundary. |
91* | Match a '9' followed by zero or more '1' characters. |
9* | Match zero or more '9' characters. |
b | End at a word boundary. |
+
quantifier matches the preceding element one or more times. It is equivalent to {1,}
. +
is a greedy quantifier whose lazy equivalent is +?
.ban+w*?b
tries to match entire words that begin with the letter a
followed by one or more instances of the letter n
. The following example illustrates this regular expression. The regular expression matches the words an
, annual
, announcement
, and antique
, and correctly fails to match autumn
and all
.Pattern | Description |
---|---|
b | Start at a word boundary. |
an+ | Match an 'a' followed by one or more 'n' characters. |
w*? | Match a word character zero or more times, but as few times as possible. |
b | End at a word boundary. |
?
quantifier matches the preceding element zero or one time. It is equivalent to {0,1}
. ?
is a greedy quantifier whose lazy equivalent is ??
.ban?b
tries to match entire words that begin with the letter a
followed by zero or one instances of the letter n
. In other words, it tries to match the words a
and an
. The following example illustrates this regular expression.Pattern | Description |
---|---|
b | Start at a word boundary. |
an? | Match an 'a' followed by zero or one 'n' character. |
b | End at a word boundary. |
{
n}
quantifier matches the preceding element exactly n times, where n https://sksadz.over-blog.com/2020/11/el-capitan-macbook-pro-early-2011.html. is any integer. {
n}
is a greedy quantifier whose lazy equivalent is {
n}?
.bd+,d{3}b
tries to match a word boundary followed by one or more decimal digits followed by three decimal digits followed by a word boundary. The following example illustrates this regular expression.Pattern | Description |
---|---|
b | Start at a word boundary. |
d+ | Match one or more decimal digits. |
, | Match a comma character. |
d{3} | Match three decimal digits. |
b | End at a word boundary. |
{
n,}
quantifier matches the preceding element at least n times, where n is any integer. {
n,}
is a greedy quantifier whose lazy equivalent is {
n,}?
.bd{2,}bD+
tries to match a word boundary followed by at least two digits followed by a word boundary and a non-digit character. The following example illustrates this regular expression. The regular expression fails to match the phrase '7 days'
because it contains just one decimal digit, but it successfully matches the phrases '10 weeks and 300 years'
.Pattern | Description |
---|---|
b | Start at a word boundary. |
d{2,} | Match at least two decimal digits. |
b | Match a word boundary. |
D+ | Match at least one non-decimal digit. |
{
n,
m}
quantifier matches the preceding element at least n times, but no more than m times, where n and m are integers. {
n,
m}
is a greedy quantifier whose lazy equivalent is {
n,
m}?
.(00s){2,4}
tries to match between two and four occurrences of two zero digits followed by a space. Note that the final portion of the input string includes this pattern five times rather than the maximum of four. However, only the initial portion of this substring (up to the space and the fifth pair of zeros) matches the regular expression pattern.*?
quantifier matches the preceding element zero or more times, but as few times as possible. It is the lazy counterpart of the greedy quantifier *
.bw*?oow*?b
matches all words that contain the string oo
.Pattern | Description |
---|---|
b | Start at a word boundary. |
w*? | Match zero or more word characters, but as few characters as possible. |
oo | Match the string 'oo'. |
w*? | Match zero or more word characters, but as few characters as possible. |
b | End on a word boundary. |
+?
Adobe zii patcher 4 3 8 cr2 free. quantifier matches the preceding element one or more times, but as few times as possible. It is the lazy counterpart of the greedy quantifier +
.bw+?b
matches one or more characters separated by word boundaries. The following example illustrates this regular expression.??
quantifier matches the preceding element zero or one time, but as few times as possible. It is the lazy counterpart of the greedy quantifier ?
.^s*(System.)??Console.Write(Line)??(??
attempts to match the strings 'Console.Write' or 'Console.WriteLine'. The string can also include 'System.' before 'Console', and it can be followed by an opening parenthesis. The string must be at the beginning of a line, although it can be preceded by white space. The following example illustrates this regular expression.Pattern | Description |
---|---|
^ | Match the start of the input stream. |
s* | Match zero or more white-space characters. |
(System.)?? | Match zero or one occurrence of the string 'System.'. |
Console.Write | Match the string 'Console.Write'. |
(Line)?? | Match zero or one occurrence of the string 'Line'. |
(?? | Match zero or one occurrence of the opening parenthesis. |
{
n}?
quantifier matches the preceding element exactly n
times, where n is any integer. It is the lazy counterpart of the greedy quantifier {
n}
.b(w{3,}?.){2}?w{3,}?b
is used to identify a Web site address. Note that it matches 'www.microsoft.com' and 'msdn.microsoft.com', but does not match 'mywebsite' or 'mycompany.com'.Pattern | Description |
---|---|
b | Start at a word boundary. |
(w{3,}?.) | Match at least 3 word characters, but as few characters as possible, followed by a dot or period character. This is the first capturing group. |
(w{3,}?.){2}? | Match the pattern in the first group two times, but as few times as possible. |
b | End the match on a word boundary. |
{
n,}?
quantifier matches the preceding element at least n
times, where n is any integer, but as few times as possible. It is the lazy counterpart of the greedy quantifier {
n,}
.{
n}?
quantifier in the previous section for an illustration. The regular expression in that example uses the {
n,}
quantifier to match a string that has at least three characters followed by a period.{
n,
m}?
quantifier matches the preceding element between n
and m
times, where n and m are integers, but as few times as possible. It is the lazy counterpart of the greedy quantifier {
n,
m}
.b[A-Z](w*?s*?){1,10}[.!?]
matches sentences that contain between one and ten words. It matches all the sentences in the input string except for one sentence that contains 18 words.Pattern | Description |
---|---|
b | Start at a word boundary. |
[A-Z] | Match an uppercase character from A to Z. |
(w*?s*?) | Match zero or more word characters, followed by one or more white-space characters, but as few times as possible. This is the first capture group. |
{1,10} | Match the previous pattern between 1 and 10 times. |
[.!?] | Match any one of the punctuation characters '.', '!', or '?'. |
?
.*
greedy quantifier is b.*([0-9]{4})b
. However, if a string contains two numbers, this regular expression matches the last four digits of the second number only, as the following example shows.*
quantifier tries to match the previous element as many times as possible in the entire string, and so it finds its match at the end of the string.*?
lazy quantifier to extract digits from both numbers, as the following example shows..
) metacharacter, which matches any character.*
, +
, and {
n,
m}
and their lazy counterparts never repeat after an empty match when the minimum number of captures has been found. This rule prevents quantifiers from entering infinite loops on empty subexpression matches when the maximum number of possible group captures is infinite or near infinite.(a?)*
, which matches zero or one 'a' character zero or more times. Note that the single capturing group captures each 'a' as well as String.Empty, but that there is no second empty match, because the first empty match causes the quantifier to stop repeating.(a1|(?(1)1)){0,2}
and (a1|(?(1)1)){2}
. Both regular expressions consist of a single capturing group, which is defined as shown in the following table.Pattern | Description |
---|---|
(a1 | Either match 'a' along with the value of the first captured group … |
|(?(1) | … or test whether the first captured group has been defined. (Note that the (?(1) construct does not define a capturing group.) |
1)) | If the first captured group exists, match its value. If the group does not exist, the group will match String.Empty. |
a1
; the {0,2}
quantifier allows only empty matches in the last iteration. https://truesload823.weebly.com/apple-mouse-serial-number.html. In contrast, the second regular expression does match 'a' because it evaluates a1
a second time; the minimum number of iterations, 2, forces the engine to repeat after an empty match.