# 正则表达式中的限定符Quantifiers in Regular Expressions

* *? 匹配零次或多次。Match zero or more times.
+ +? 匹配一次或多次。Match one or more times.
? ?? 匹配零次或一次。Match zero or one time.
{ n }{ n } { n }?{ n }? 恰好匹配 n 次 。Match exactly n times.
{ n ,}{ n ,} { n ,}?{ n ,}? 至少匹配 n 次 。Match at least n times.
{ n , m }{ n , m } { n , m }?{ n , m }? 匹配 n 到 m 次 。Match from n to m times.

## 正则表达式限定符Regular Expression Quantifiers

### 匹配零次或多次：*Match Zero or More Times: *

* 限定符与前面的元素匹配零次或多次。The * quantifier matches the preceding element zero or more times. 它相当于 {0,} 量符。It is equivalent to the {0,} quantifier. * 是贪婪量符，相当的惰性量符是 *?* is a greedy quantifier whose lazy equivalent is *?.

string pattern = @"\b91*9*\b";
string input = "99 95 919 929 9119 9219 999 9919 91119";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

// The example displays the following output:
//       '99' found at position 0.
//       '919' found at position 6.
//       '9119' found at position 14.
//       '999' found at position 24.
//       '91119' found at position 33.

Dim pattern As String = "\b91*9*\b"
Dim input As String = "99 95 919 929 9119 9219 999 9919 91119"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '99' found at position 0.
'       '919' found at position 6.
'       '9119' found at position 14.
'       '999' found at position 24.
'       '91119' found at position 33.


\b 在单词边界处开始。Start at a word boundary.
91* 匹配后跟零个或多个“1”字符的“9”。Match a "9" followed by zero or more "1" characters.
9* 匹配零个或多个“9”字符。Match zero or more "9" characters.
\b 在字边界结束。End at a word boundary.

### 匹配一次或多次：+Match One or More Times: +

+ 量符匹配上一元素一次或多次。The + quantifier matches the preceding element one or more times. 它相当于 {1,}It is equivalent to {1,}. + 是贪婪量符，相当的惰性量符是 +?+ is a greedy quantifier whose lazy equivalent is +?.

string pattern = @"\ban+\w*?\b";

string input = "Autumn is a great time for an annual announcement to all antique collectors.";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

// The example displays the following output:
//       'an' found at position 27.
//       'annual' found at position 30.
//       'announcement' found at position 37.
//       'antique' found at position 57.

Dim pattern As String = "\ban+\w*?\b"

Dim input As String = "Autumn is a great time for an annual announcement to all antique collectors."
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'an' found at position 27.
'       'annual' found at position 30.
'       'announcement' found at position 37.
'       'antique' found at position 57.


\b 在单词边界处开始。Start at a word boundary.
an+ 匹配后跟一个或多个“n”字符的“a”。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.

### 匹配零次或一次：?Match Zero or One Time: ?

? 量符匹配上一元素零次或一次。The ? quantifier matches the preceding element zero or one time. 它相当于 {0,1}It is equivalent to {0,1}. ? 是贪婪量符，相当的惰性量符是 ??? is a greedy quantifier whose lazy equivalent is ??.

string pattern = @"\ban?\b";
string input = "An amiable animal with a large snount and an animated nose.";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

// The example displays the following output:
//        'An' found at position 0.
//        'a' found at position 23.
//        'an' found at position 42.

Dim pattern As String = "\ban?\b"
Dim input As String = "An amiable animal with a large snount and an animated nose."
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'An' found at position 0.
'       'a' found at position 23.
'       'an' found at position 42.


\b 在单词边界处开始。Start at a word boundary.
an? 匹配后跟零个或一个“n”字符的“a”。Match an "a" followed by zero or one "n" character.
\b 在字边界结束。End at a word boundary.

### 恰好匹配 n 次：{n}Match Exactly n Times: {n}

{n} 限定符与前面的元素恰好匹配 n 次，其中 n 是任何整数 。The {n} quantifier matches the preceding element exactly n times, where n is any integer. {n} 是贪婪限定符，其惰性等效项是 {n}?{n} is a greedy quantifier whose lazy equivalent is {n}?.

string pattern = @"\b\d+\,\d{3}\b";
string input = "Sales totaled 103,524 million in January, " +
"106,971 million in February, but only " +
"943 million in March.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        '103,524' found at position 14.
//        '106,971' found at position 45.

Dim pattern As String = "\b\d+\,\d{3}\b"
Dim input As String = "Sales totaled 103,524 million in January, " + _
"106,971 million in February, but only " + _
"943 million in March."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '103,524' found at position 14.
'       '106,971' found at position 45.


\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 次：{n,}Match at Least n Times: {n,}

{n,} 限定符与前面的元素至少匹配 n 次，其中 n 是任何整数 。The {n,} quantifier matches the preceding element at least n times, where n is any integer. {n,} 是贪婪限定符，其惰性等效项是 {n,}?{n,} is a greedy quantifier whose lazy equivalent is {n,}?.

string pattern = @"\b\d{2,}\b\D+";
string input = "7 days, 10 weeks, 300 years";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        '10 weeks, ' found at position 8.
//        '300 years' found at position 18.

 Dim pattern As String = "\b\d{2,}\b\D+"
Dim input As String = "7 days, 10 weeks, 300 years"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '10 weeks, ' found at position 8.
'       '300 years' found at position 18.


\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 次：{n,m}Match Between n and m Times: {n,m}

{n,m} 限定符与前面的元素至少匹配 n 次，但不超过 m 次，其中 n 和 m 是整数 。The {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} 是贪婪限定符，相当的惰性限定符是 {n,m}?{n,m} is a greedy quantifier whose lazy equivalent is {n,m}?.

string pattern = @"(00\s){2,4}";
string input = "0x00 FF 00 00 18 17 FF 00 00 00 21 00 00 00 00 00";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        '00 00 ' found at position 8.
//        '00 00 00 ' found at position 23.
//        '00 00 00 00 ' found at position 35.

Dim pattern As String = "(00\s){2,4}"
Dim input As String = "0x00 FF 00 00 18 17 FF 00 00 00 21 00 00 00 00 00"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       '00 00 ' found at position 8.
'       '00 00 00 ' found at position 23.
'       '00 00 00 00 ' found at position 35.


### 匹配零次或多次（惰性匹配）：*?Match Zero or More Times (Lazy Match): *?

*? 量符匹配上一元素零次或多次，但次数尽可能少。The *? quantifier matches the preceding element zero or more times, but as few times as possible. 它是贪婪量符 * 对应的惰性量符。It is the lazy counterpart of the greedy quantifier *.

 string pattern = @"\b\w*?oo\w*?\b";
string input = "woof root root rob oof woo woe";
foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'woof' found at position 0.
//        'root' found at position 5.
//        'root' found at position 10.
//        'oof' found at position 19.
//        'woo' found at position 23.

 Dim pattern As String = "\b\w*?oo\w*?\b"
Dim input As String = "woof root root rob oof woo woe"
For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'woof' found at position 0.
'       'root' found at position 5.
'       'root' found at position 10.
'       'oof' found at position 19.
'       'woo' found at position 23.


\b 在单词边界处开始。Start at a word boundary.
\w*? 匹配零个或多个单词字符，但字符要尽可能的少。Match zero or more word characters, but as few characters as possible.
oo 匹配字符串“oo”。Match the string "oo".
\w*? 匹配零个或多个单词字符，但字符要尽可能的少。Match zero or more word characters, but as few characters as possible.
\b 在单词边界处结束。End on a word boundary.

### 匹配一次或多次（惰性匹配）：+?Match One or More Times (Lazy Match): +?

+? 量符匹配上一元素一次或多次，但次数尽可能少。The +? quantifier matches the preceding element one or more times, but as few times as possible. 它是贪婪量符 + 对应的惰性量符。It is the lazy counterpart of the greedy quantifier +.

string pattern = @"\b\w+?\b";
string input = "Aa Bb Cc Dd Ee Ff";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'Aa' found at position 0.
//        'Bb' found at position 3.
//        'Cc' found at position 6.
//        'Dd' found at position 9.
//        'Ee' found at position 12.
//        'Ff' found at position 15.

 Dim pattern As String = "\b\w+?\b"
Dim input As String = "Aa Bb Cc Dd Ee Ff"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'Aa' found at position 0.
'       'Bb' found at position 3.
'       'Cc' found at position 6.
'       'Dd' found at position 9.
'       'Ee' found at position 12.
'       'Ff' found at position 15.


### 匹配零次或一次（惰性匹配）：??Match Zero or One Time (Lazy Match): ??

?? 量符匹配上一元素零次或一次，但次数尽可能少。The ?? quantifier matches the preceding element zero or one time, but as few times as possible. 它是贪婪量符 ? 对应的惰性量符。It is the lazy counterpart of the greedy quantifier ?.

string pattern = @"^\s*(System.)??Console.Write(Line)??\(??";
string input = "System.Console.WriteLine(\"Hello!\")\n" +
"Console.Write(\"Hello!\")\n" +
"Console.WriteLine(\"Hello!\")\n" +
"   Console.WriteLine";
foreach (Match match in Regex.Matches(input, pattern,
RegexOptions.IgnorePatternWhitespace |
RegexOptions.IgnoreCase |
RegexOptions.Multiline))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'System.Console.Write' found at position 0.
//        'Console.Write' found at position 36.
//        'Console.Write' found at position 61.
//        '   Console.Write' found at position 110.

Dim pattern As String = "^\s*(System.)??Console.Write(Line)??\(??"
Dim input As String = "System.Console.WriteLine(""Hello!"")" + vbCrLf + _
"Console.Write(""Hello!"")" + vbCrLf + _
"Console.WriteLine(""Hello!"")" + vbCrLf + _
"   Console.WriteLine"
For Each match As Match In Regex.Matches(input, pattern, _
RegexOptions.IgnorePatternWhitespace Or RegexOptions.IgnoreCase Or RegexOptions.MultiLine)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'System.Console.Write' found at position 0.
'       'Console.Write' found at position 36.
'       'Console.Write' found at position 61.
'       '   Console.Write' found at position 110.


^ 匹配输入流的开头。Match the start of the input stream.
\s* 匹配零个或多个空白字符。Match zero or more white-space characters.
(System.)?? 匹配字符串“System.”的零个或一个匹配项。Match zero or one occurrence of the string "System.".
Console.Write 匹配字符串“Console.Write”。Match the string "Console.Write".
(Line)?? 匹配字符串“Line”的零个或一个匹配项。Match zero or one occurrence of the string "Line".
\(?? 匹配左括号的零个或一个匹配项。Match zero or one occurrence of the opening parenthesis.

### 恰好匹配 n 次（惰性匹配）：{n}?Match Exactly n Times (Lazy Match): {n}?

{n}? 限定符与前面的元素恰好匹配 n 次，其中 n 是任何整数 。The {n}? quantifier matches the preceding element exactly n times, where n is any integer. 它是贪婪限定符 {n} 的惰性对应项 。It is the lazy counterpart of the greedy quantifier {n}.

string pattern = @"\b(\w{3,}?\.){2}?\w{3,}?\b";
string input = "www.microsoft.com msdn.microsoft.com mywebsite mycompany.com";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'www.microsoft.com' found at position 0.
//        'msdn.microsoft.com' found at position 18.

 Dim pattern As String = "\b(\w{3,}?\.){2}?\w{3,}?\b"
Dim input As String = "www.microsoft.com msdn.microsoft.com mywebsite mycompany.com"
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'www.microsoft.com' found at position 0.
'       'msdn.microsoft.com' found at position 18.


\b 在单词边界处开始。Start at a word boundary.
(\w{3,}?\.) 匹配至少 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 次（惰性匹配）：{n,}?Match at Least n Times (Lazy Match): {n,}?

{n,}? 限定符与前面的元素至少匹配 n 次，其中 n 是任何整数，但次数尽可能少 。The {n,}? quantifier matches the preceding element at least n times, where n is any integer, but as few times as possible. 它是贪婪限定符 {n,} 的惰性对应项 。It is the lazy counterpart of the greedy quantifier {n,}.

### 匹配 n 到 m 次（惰性匹配）：{n,m}?Match Between n and m Times (Lazy Match): {n,m}?

{n,m}? 限定符匹配上一元素 n 次到 m 次，其中 n 和 m 是整数，但次数尽可能少 。The {n,m}? quantifier matches the preceding element between n and m times, where n and m are integers, but as few times as possible. 它是贪婪限定符 {n,m} 的惰性对应项 。It is the lazy counterpart of the greedy quantifier {n,m}.

string pattern = @"\b[A-Z](\w*?\s*?){1,10}[.!?]";
string input = "Hi. I am writing a short note. Its purpose is " +
"to test a regular expression that attempts to find " +
"sentences with ten or fewer words. Most sentences " +
"in this note are short.";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index);

//  The example displays the following output:
//        'Hi.' found at position 0.
//        'I am writing a short note.' found at position 4.
//        'Most sentences in this note are short.' found at position 132.

Dim pattern As String = "\b[A-Z](\w*\s?){1,10}?[.!?]"
Dim input As String = "Hi. I am writing a short note. Its purpose is " + _
"to test a regular expression that attempts to find " + _
"sentences with ten or fewer words. Most sentences " + _
"in this note are short."
For Each match As Match In Regex.Matches(input, pattern)
Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index)
Next
' The example displays the following output:
'       'Hi.' found at position 0.
'       'I am writing a short note.' found at position 4.
'       'Most sentences in this note are short.' found at position 132.


\b 在单词边界处开始。Start at a word boundary.
[A-Z] 匹配从 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} 与前面的模式匹配 1 到 10 次。Match the previous pattern between 1 and 10 times.
[.!?] 匹配标点字符“.”、“!”或“?”中的任何一种。Match any one of the punctuation characters ".", "!", or "?".

## 贪婪与惰性限定符Greedy and Lazy Quantifiers

• 贪婪版本。A greedy version.

贪婪限定符尝试尽可能多地匹配元素。A greedy quantifier tries to match an element as many times as possible.

• 非贪婪（或惰性）版本。A non-greedy (or lazy) version.

非贪婪限定符尝试尽可能少地匹配元素。A non-greedy quantifier tries to match an element as few times as possible. 只需添加 ?，即可将贪婪量符转换为惰性量符。You can turn a greedy quantifier into a lazy quantifier by simply adding a ?.

string greedyPattern = @"\b.*([0-9]{4})\b";
string input1 = "1112223333 3992991999";
foreach (Match match in Regex.Matches(input1, greedyPattern))
Console.WriteLine("Account ending in ******{0}.", match.Groups.Value);

// The example displays the following output:
//       Account ending in ******1999.

Dim greedyPattern As String = "\b.*([0-9]{4})\b"
Dim input1 As String = "1112223333 3992991999"
For Each match As Match In Regex.Matches(input1, greedypattern)
Console.WriteLine("Account ending in ******{0}.", match.Groups(1).Value)
Next
' The example displays the following output:
'       Account ending in ******1999.


string lazyPattern = @"\b.*?([0-9]{4})\b";
string input2 = "1112223333 3992991999";
foreach (Match match in Regex.Matches(input2, lazyPattern))
Console.WriteLine("Account ending in ******{0}.", match.Groups.Value);

// The example displays the following output:
//       Account ending in ******3333.
//       Account ending in ******1999.

Dim lazyPattern As String = "\b.*?([0-9]{4})\b"
Dim input2 As String = "1112223333 3992991999"
For Each match As Match In Regex.Matches(input2, lazypattern)
Console.WriteLine("Account ending in ******{0}.", match.Groups(1).Value)
Next
' The example displays the following output:
'       Account ending in ******3333.
'       Account ending in ******1999.


## 限定符和空匹配项Quantifiers and Empty Matches

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern = "(a?)*";
string input = "aaabbb";
Match match = Regex.Match(input, pattern);
Console.WriteLine("Match: '{0}' at index {1}",
match.Value, match.Index);
if (match.Groups.Count > 1) {
GroupCollection groups = match.Groups;
for (int grpCtr = 1; grpCtr <= groups.Count - 1; grpCtr++) {
Console.WriteLine("   Group {0}: '{1}' at index {2}",
grpCtr,
groups[grpCtr].Value,
groups[grpCtr].Index);
int captureCtr = 0;
foreach (Capture capture in groups[grpCtr].Captures) {
captureCtr++;
Console.WriteLine("      Capture {0}: '{1}' at index {2}",
captureCtr, capture.Value, capture.Index);
}
}
}
}
}
// The example displays the following output:
//       Match: 'aaa' at index 0
//          Group 1: '' at index 3
//             Capture 1: 'a' at index 0
//             Capture 2: 'a' at index 1
//             Capture 3: 'a' at index 2
//             Capture 4: '' at index 3

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
Dim pattern As String = "(a?)*"
Dim input As String = "aaabbb"
Dim match As Match = Regex.Match(input, pattern)
Console.WriteLine("Match: '{0}' at index {1}",
match.Value, match.Index)
If match.Groups.Count > 1 Then
Dim groups As GroupCollection = match.Groups
For grpCtr As Integer = 1 To groups.Count - 1
Console.WriteLine("   Group {0}: '{1}' at index {2}",
grpCtr,
groups(grpCtr).Value,
groups(grpCtr).Index)
Dim captureCtr As Integer = 0
For Each capture As Capture In groups(grpCtr).Captures
captureCtr += 1
Console.WriteLine("      Capture {0}: '{1}' at index {2}",
captureCtr, capture.Value, capture.Index)
Next
Next
End If
End Sub
End Module
' The example displays the following output:
'       Match: 'aaa' at index 0
'          Group 1: '' at index 3
'             Capture 1: 'a' at index 0
'             Capture 2: 'a' at index 1
'             Capture 3: 'a' at index 2
'             Capture 4: '' at index 3


(a\1 匹配“a”以及第一个捕获组的值...Either match "a" along with the value of the first captured group …
|(?(1) 或测试是否定义了第一个捕获组。or test whether the first captured group has been defined. （请注意，(?(1) 构造不定义捕获组。）(Note that the (?(1) construct does not define a capturing group.)
\1)) 如果第一个捕获组存在，则匹配其值。If the first captured group exists, match its value. 如果组不存在，组会匹配 String.EmptyIf the group does not exist, the group will match String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
public static void Main()
{
string pattern, input;

pattern = @"(a\1|(?(1)\1)){0,2}";
input = "aaabbb";

Console.WriteLine("Regex pattern: {0}", pattern);
Match match = Regex.Match(input, pattern);
Console.WriteLine("Match: '{0}' at position {1}.",
match.Value, match.Index);
if (match.Groups.Count > 1) {
for (int groupCtr = 1; groupCtr <= match.Groups.Count - 1; groupCtr++)
{
Group group = match.Groups[groupCtr];
Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
groupCtr, group.Value, group.Index);
int captureCtr = 0;
foreach (Capture capture in group.Captures) {
captureCtr++;
Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
captureCtr, capture.Value, capture.Index);
}
}
}
Console.WriteLine();

pattern = @"(a\1|(?(1)\1)){2}";
Console.WriteLine("Regex pattern: {0}", pattern);
match = Regex.Match(input, pattern);
Console.WriteLine("Matched '{0}' at position {1}.",
match.Value, match.Index);
if (match.Groups.Count > 1) {
for (int groupCtr = 1; groupCtr <= match.Groups.Count - 1; groupCtr++)
{
Group group = match.Groups[groupCtr];
Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
groupCtr, group.Value, group.Index);
int captureCtr = 0;
foreach (Capture capture in group.Captures) {
captureCtr++;
Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
captureCtr, capture.Value, capture.Index);
}
}
}
}
}
// The example displays the following output:
//       Regex pattern: (a\1|(?(1)\1)){0,2}
//       Match: '' at position 0.
//          Group: 1: '' at position 0.
//             Capture: 1: '' at position 0.
//
//       Regex pattern: (a\1|(?(1)\1)){2}
//       Matched 'a' at position 0.
//          Group: 1: 'a' at position 0.
//             Capture: 1: '' at position 0.
//             Capture: 2: 'a' at position 0.

Imports System.Text.RegularExpressions

Module Example
Public Sub Main()
Dim pattern, input As String

pattern = "(a\1|(?(1)\1)){0,2}"
input = "aaabbb"

Console.WriteLine("Regex pattern: {0}", pattern)
Dim match As Match = Regex.Match(input, pattern)
Console.WriteLine("Match: '{0}' at position {1}.",
match.Value, match.Index)
If match.Groups.Count > 1 Then
For groupCtr As Integer = 1 To match.Groups.Count - 1
Dim group As Group = match.Groups(groupCtr)
Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
groupCtr, group.Value, group.Index)
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
captureCtr += 1
Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
captureCtr, capture.Value, capture.Index)
Next
Next
End If
Console.WriteLine()

pattern = "(a\1|(?(1)\1)){2}"
Console.WriteLine("Regex pattern: {0}", pattern)
match = Regex.Match(input, pattern)
Console.WriteLine("Matched '{0}' at position {1}.",
match.Value, match.Index)
If match.Groups.Count > 1 Then
For groupCtr As Integer = 1 To match.Groups.Count - 1
Dim group As Group = match.Groups(groupCtr)
Console.WriteLine("   Group: {0}: '{1}' at position {2}.",
groupCtr, group.Value, group.Index)
Dim captureCtr As Integer = 0
For Each capture As Capture In group.Captures
captureCtr += 1
Console.WriteLine("      Capture: {0}: '{1}' at position {2}.",
captureCtr, capture.Value, capture.Index)
Next
Next
End If
End Sub
End Module
' The example displays the following output:
'       Regex pattern: (a\1|(?(1)\1)){0,2}
'       Match: '' at position 0.
'          Group: 1: '' at position 0.
'             Capture: 1: '' at position 0.
'
'       Regex pattern: (a\1|(?(1)\1)){2}
'       Matched 'a' at position 0.
'          Group: 1: 'a' at position 0.
'             Capture: 1: '' at position 0.
'             Capture: 2: 'a' at position 0.