共用方式為


數量詞

更新: 2008 年 7 月

數量詞會指定前面的項目 (可能是字元、群組或字元類別) 必須出現在輸入的執行個體數,才算是符合的項目。

.NET Framework 規則運算式中的數量詞

下表列出 .NET Framework 規則運算式支援的數量詞。數量 n 和 m 是整數常數。如需窮盡 (Greedy) 和省略 (Lazy) 數量詞的差異說明,請參閱下表之後的「窮盡與省略數量詞」一節。

數量詞

說明

*

比對前置項目零次或多次。它就相當於 {0,}。* 是窮盡數量詞,非窮盡 (Non-Greedy) 的對等用法為 *?。

例如,規則運算式 \b91*9*\b 會嘗試比對接在字組界限 (Word Boundary) 後的數字 9。9 後面可以接著零個或多個數字 1,而這後面又可以接著零個或多個數字 9。下列範例說明這個規則運算式。在輸入字串的 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.

+

比對前置項目一次或多次。它就相當於 {1,}。+ 是窮盡數量詞,非窮盡的對等用法為 +?。

例如,規則運算式 \ba(n)+\w*?\b 會嘗試比對開頭字母為 a 且後面接著一個或多個字母 n 的單字。下列範例說明這個規則運算式。這個規則運算式會比對出 an、annual、announcement 和 antique 等單字,但比對不出 autumn 和 all。

Dim pattern As String = "\ba(n)+\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 = @"\ba(n)+\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.

?

比對前置項目零次或一次。它就相當於 {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.

{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.

{n,}

比對前置項目至少 n 次。{n,} 是窮盡數量詞,非窮盡的對等用法為 {n}?。

例如,規則運算式 \b\d{2,}\b\D+ 會嘗試比對字組界限,後面接著至少兩個數字,再接著字組界限和一個非數字字元。下列範例說明這個規則運算式。這個規則運算式比對不出 7 days 這個詞組,因為它只有一個十進位數字,但可以比對出 10 weeks 和 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.

{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+?\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.

{n}?

比對前置項目剛好 n 次。它是對應於窮盡數量詞 {n}+ 的省略數量詞。

例如,規則運算式 \b(\w{3,}?\.){2}?\w{3,}?\b 會比對剛好兩組字元,後面接著句點字組界限。後面會再接著另一組字元和字組界限。這個規則運算式應能識別網站的位址。下列範例說明這個規則運算式。請注意,這會比對 www.microsoft.com 和 mdsn.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.

{n,}?

比對前置項目至少 n 次,但越少次越好。它是對應於窮盡數量詞 {n,} 的省略數量詞。

說明請參閱 {n}? 數量詞的範例。該範例的規則運算式會使用 {n,} 數量詞來比對至少有三個字元且後面接著句點的字串。

{n,m}?

比對前置項目介於 n 和 m 次,但越少次越好。它是對應於窮盡數量詞 {n,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.*([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.

在大部分情況下,使用窮盡和省略數量詞的規則運算式會傳回相同的結果。一般是在與比對任何字元的句點 ( . ) 中繼字元搭配使用時,才會傳回不同的結果。

請參閱

其他資源

規則運算式語言項目

變更記錄

日期

記錄

原因

2008 年 7 月

廣泛修訂。

內容 Bug 修正。