數量詞
更新:2010 年 10 月
數量詞會指定有多少個字元、群組或字元類別的執行個體必須存在於輸入中,才能讓比對找到。 下表列出 .NET Framework 支援的數量詞。
窮盡數量詞 |
省略數量詞 |
描述 |
---|---|---|
* |
*? |
比對零次或多次。 |
+ |
+? |
比對一次或多次。 |
? |
?? |
比對零次或一次。 |
{n} |
{n}? |
正好比對 n 次。 |
{n,} |
{n,}? |
比對至少 n 次。 |
{n,m} |
{n,m}? |
比對從 n 到 m 次。 |
數量 n 和 m 是整數常數。 通常,數量詞是窮盡的,它們會讓規則運算式引擎盡可能多次比對特定的模式。 將 ? 字元附加至限定詞會使它變得省略,它會讓規則運算式引擎盡可能比對最少次數。 如需窮盡和省略數量詞之間差異的完整說明,請參閱本主題稍後的窮盡和省略數量詞一節。
![]() |
---|
巢狀數量詞 (例如,規則運算式模式(a*)*) 可以增加必須執行規則運算式引擎的比較數目,做為輸入字串中字元數目的指數函式。如需關於此行為及其解決方法的詳細資訊,請參閱回溯。 |
規則運算式數量詞
下一節列出 .NET Framework 規則運算式所支援的數量詞。
![]() |
---|
如果在規則運算式模式中遇到 *、+、?、{ 和 } 字元,規則運算式引擎會將它們解譯為數量詞或數量詞建構的一部分,除非它們包含在字元類別中。若要將這些解譯為字元類別外部的常值字元,您必須在前面以反斜線溢出它們。例如,在規則運算式模式中的字串 \* 會被解譯為常值的星號 ("*") 字元。 |
比對零次或多次:*
* 數量詞會比對前置項目零次或多次。 它相當於 {0,} 數量詞。 * 是窮盡數量詞,其省略的對等用法為 *?。
下列範例說明這個規則運算式。 在輸入字串的 9 組數字中,五個符合模式,而四個 (95、929、9129 和 9919) 則不符合。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
91* |
比對後接零個或多個 "1" 字元的 "9"。 |
9* |
比對零個或多個 "9" 字元。 |
\b |
在字緣結束。 |
比對一次或多次:+
+ 數量詞會比對前置項目一次或多次。 它就相當於 {1,}。 + 是窮盡數量詞,其省略的對等用法為 +?。
例如,規則運算式 \ban+\w*?\b 會嘗試比對開頭字母為 a 且後面接著一個或多個字母 n 的單字。 下列範例說明這個規則運算式。 這個規則運算式會比對出 an、annual、announcement 和 antique 等單字,但比對不出 autumn 和 all。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
an+ |
比對後接一個或多個 "n" 字元的 "a"。 |
\w*? |
比對字組字元零次或多次,但盡可能減少次數。 |
\b |
在字緣結束。 |
比對零次或一次:?
? 數量詞會比對前置項目零次或一次。 它就相當於 {0,1}。 ? 是窮盡數量詞,其省略的對等用法為 ??。
例如,規則運算式 \ban?\b 會嘗試比對開頭字母為 a 且後面接著零個或多個字母 n 的單字。 換句話說,它會嘗試比對單字 a 和 an。 下列範例說明這個規則運算式。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
an? |
比對後接零個或一個 "n" 字元的 "a"。 |
\b |
在字緣結束。 |
比對正好 n 次:{n}
{n} 數量詞會比對前置項目剛好 n 次,其中 n 可以是任何整數。 {n} 是窮盡數量詞,其省略的對等用法為 {n}?。
例如,規則運算式 \b\d+\,\d{3}\b 會嘗試比對字組界限,後面接著一個或多個十進位數字,再接著三個十進位數字,最後接著字組界限。 下列範例說明這個規則運算式。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
\d+ |
比對一個或多個十進位數字。 |
\, |
比對逗號字元。 |
\d{3} |
比對三個十進位數字。 |
\b |
在字緣結束。 |
比對至少 n 次:{n,}
{n,} 數量詞會比對前置項目至少 n 次,其中 n 可以是任何整數。 {n,} 是窮盡數量詞,其省略的對等用法為 {n}?。
例如,規則運算式 \b\d{2,}\b\D+ 會嘗試比對字組界限,後面接著至少兩個數字,再接著字組界限和一個非數字字元。 下列範例說明這個規則運算式。 此規則運算式比對不出 "7 days" 詞組,因為它只有一個十進位數字,但可以成功比對出 "10 weeks and 300 years" 詞組。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
\d{2,} |
比對至少兩個十進位數字。 |
\b |
比對字緣。 |
\D+ |
比對至少一個非十進位數字。 |
比對 n 和 m 之間的次數:{n,m}
{n,m} 數量詞會比對前置項目至少 n 次,但不會超過 m 次,其中 n 與 m 可以是任何整數。 {n,m} 是窮盡數量詞,其省略的對等用法為 {n,m}?。
在下列範例中,規則運算式 (00\s){2,4} 會嘗試比對出現兩次到四次的兩個零,後接一個空格。 請注意,輸入字串的最後部分包含此模式五次,而非四次上限。 不過,只有這個子字串的開頭部分 (直到空格和第五組零) 符合規則運算式模式。
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.
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.
比對零次或多次 (省略比對):*?
*? 數量詞會比對前置項目零次或多次,但會盡可能減少次數。 它是窮盡數量詞 * 的對應省略數量詞。
在下列範例中,規則運算式 \b\w*?oo\w*?\b會比對包含 oo 字串的所有字組。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
\w*? |
比對零個或多個字組字元,但越少字元越好。 |
oo |
比對字串 "oo"。 |
\w*? |
比對零個或多個字組字元,但越少字元越好。 |
\b |
在字組界限上結束。 |
比對一次或多次 (省略比對):+?
+? 數量詞會比對前置項目一次或多次,但會盡可能減少次數。 它是窮盡數量詞 + 的對應省略數量詞。
例如,規則運算式 \b\w+?\b 會比對由字組界限隔開的一個或多個字元。 下列範例說明這個規則運算式。
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.
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.
比對零次或一次 (省略比對):??。
?? 數量詞會比對前置項目零次或一次,但會盡可能減少次數。 它是窮盡數量詞 ? 的對應省略數量詞。
例如,一般運算式^\s*(System.)??Console.Write(Line)??\(??會嘗試比對字串串"Console.Write" 或"Console.WriteLine"。 此字串也可在 "Console" 之前包含 "System.",而且後面可以接著左括號。 此字串必須位於行首,但前面可以是空格。 下列範例說明這個規則運算式。
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.ReadLine()" + 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.
string pattern = @"^\s*(System.)??Console.Write(Line)??\(??";
string input = "System.Console.WriteLine(\"Hello!\")\n" +
"Console.Write(\"Hello!\")\n" +
"Console.WriteLine(\"Hello!\")\n" +
"Console.ReadLine()\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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
^ |
比對輸入資料流的開頭。 |
\s* |
比對零個以上的空白字元。 |
(System.)?? |
比對出現零次或一次的 "System" 字串。 |
Console.Write |
比對字串 "Console.Write"。 |
(Line)?? |
比對零個或一個符合 "Line" 字串的項目。 |
\(?? |
比對出現零次或一次的左括號。 |
比對至少 n 次 (省略比對):{n}?
{n}? 數量詞會比對前置項目剛好 n 次,其中 n 可以是任何整數。 它是窮盡數量詞 {n}+ 的對應省略數量詞。
在廈列範例中,規則運算式 \b(\w{3,}?\.){2}?\w{3,}?\b用於識別網址。 請注意,它會比對 "www.microsoft.com" 和 "msdn.microsoft.com",但不會比對 "mywebsite" 或 "mycompany.com"。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
(\w{3,}? \.) |
比對至少 3 個字組字元,但盡可能減少次數,後接點或句號字元。 這是第一個擷取群組。 |
(\w{3,}? \.){2}? |
比對第一個群組中的模式兩次,但為盡可能減少次數。 |
\b |
結束字緣比對。 |
比對至少 n 次 (省略比對):{n,}?
{n,}? 數量詞會比對前置項目至少 n 次,其中 n 可以是任何整數,但會盡可能減少次數。 它是窮盡數量詞 {n,} 的對應省略數量詞。
如需圖例,請參閱上一節中的 {n}? 數量詞。 該範例的規則運算式會使用 { ,} 數量詞來比對至少有三個字元並後接句點的字串。
比對 n 和 m 之間的次數 (省略比對):{n,m}?
{n,m}? 數量詞會比對前置項目 n 到 m 次,其中 n 與 m 是整數,但會盡可能減少次數。 它是窮盡數量詞 {,m} 的對應省略數量詞。
在下列範例中,規則運算式 \b[A-Z](\w*\s+){1,10}?[.!?] 會在包含一個到十個字的句子之間比對。 它會比對輸入字串中的所有句子,但包含 18 個字組的單一句子除外。
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.
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.
此規則運算式模式的定義方式如下表所示。
模式 |
描述 |
---|---|
\b |
從字緣開始。 |
[A-Z] |
比對從 A 到 Z 範圍的一個大寫字元。 |
(\w*\s+) |
比對零個或多個字組字元,後接一個或多個空格字元。 這是第一個擷取群組。 |
{1,10}? |
以 1 和 10 之間的次數比對上一個模式,但盡可能減少次數。 |
[.!?] |
比對任何一個標點符號字元 "."、"!" 或 "?"。 |
窮盡與省略數量詞
許多數量詞都有兩種版本:
窮盡版本
窮盡數量詞會嘗試盡可能多次比對項目。
非窮盡 (或省略) 版本
非窮盡數量詞會嘗試盡可能減少比對項目的次數。 您可以藉由只新增 ?,將窮盡數量詞轉變成靜態數量詞。
請考慮簡單的規則運算式,可擷取數字字串 (例如信用卡號碼) 的後四位數字。 使用 * 窮盡數量詞的規則運算式版本為 \b.*([0-9]{4})\b。 不過,如果字串包含兩個數字,此規則運算式就只會比對第二個數字的最後四個位數,如下列範例所示。
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 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[1].Value);
// The example displays the following output:
// 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.
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[1].Value);
// The example displays the following output:
// Account ending in ******3333.
// Account ending in ******1999.
在大部分情況下,使用窮盡和省略數量詞的規則運算式會傳回相同的結果。 它們一般是在與符合任何字元的萬用 ( . 中繼字元搭配使用時,才會傳回不同的結果。
數量詞和空白比對
當數量詞 *、+ 和 {n,m},以及其惰性對應詞找到最小數量的擷取項目時,就永遠不會在空白比對後重複。 當可能擷取的最大群組數是無限大或接近無限大時,這項規則會防止數量詞在空的子運算式比對上進入無限迴圈。
例如,下列程式碼顯示以一般運算式模式 (a?)* 叫用 Regex.Match 方法的結果,該模式有零次以上符合零或一個 "a" 字元。 請注意,單一擷取群組會擷取每個 "a" 以及String.Empty,但沒有第二個空的相符項目,因為第一個空的相符項目會導致數量詞停止重複。
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
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
若要查看定義最小及最大擷取數量之擷取群組與定義固定擷取數量之擷取群組間的實際差異,請考慮規則運算式模式 (a\1|(?(1)\1)){0,2} 和 (a\1|(?(1)\1)){2}。 兩個規則運算式都包含單一擷取群組,其定義如下表所示。
模式 |
描述 |
---|---|
(a\1 |
比對 "a" 和第一個擷取群組的值... |
|(?(1) |
… 或測試是否已定義第一個擷取到的群組。 (請注意,(?(1)構建未定義擷取群組。) |
\1)) |
如果第一個擷取群組存在,則比對其值。 如果群組不存在,該群組會符合String.Empty。 |
第一個規則運算式嘗試比對此模式零次和兩次;第二個比對正好兩次。 因為第一種模式達到其最小的擷取數目與第一個 String.Empty 擷取,因此不會重複嘗試比對 a\1;{0,2} 數量詞只允許最後一個反覆項目中的空符合項目。 相反地,因為會評估a\1第二次,因此第二個一般運算式確實會比對 "a";而反覆運算的最小數值 2 會強制引擎在空白比對後重複。
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.
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.
請參閱
概念
變更記錄
日期 |
記錄 |
原因 |
---|---|---|
2010 年 10 月 |
新增「數量詞和空白比對」章節。 |
資訊加強。 |