GroupCollection.Item[] 屬性

定義

啟用依據整數或字串索引來存取集合的單一成員。

多載

Item[Int32]

以整數索引啟用對集合成員的存取。

Item[String]

以字串索引啟用對集合成員的存取。

Item[Int32]

Source:
GroupCollection.cs
Source:
GroupCollection.cs
Source:
GroupCollection.cs

以整數索引啟用對集合成員的存取。

public:
 property System::Text::RegularExpressions::Group ^ default[int] { System::Text::RegularExpressions::Group ^ get(int groupnum); };
public System.Text.RegularExpressions.Group this[int groupnum] { get; }
member this.Item(int) : System.Text.RegularExpressions.Group
Default Public ReadOnly Property Item(groupnum As Integer) As Group

參數

groupnum
Int32

要擷取之集合成員的以零起始的索引。

屬性值

groupnum 指定的集合成員。

實作

範例

下列範例會定義包含兩個編號群組的正則運算式。 第一個群組會擷取一或多個連續的數位。 第二個群組符合單一字元。 因為正則運算式引擎會尋找第一個群組的零或一個出現專案,所以即使正則運算式比對成功,也不會一定會找到相符專案。 接著,此範例說明當 屬性用來擷取不相符的群組、相符群組,以及正則運算式中未定義的群組時 Item[Int32] 的結果。 此範例會定義正則運算式模式 (\d+)*(\w)\2 ,其解譯方式如下表所示。

模式 描述
(\d+)* 比對一或多個出現十進位數。 這是第一個擷取群組。 比對此模式為零或一次。
(\w) 這是第二個擷取群組。
\k 比對第二個擷取群組所擷取的字串。
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\d+)*(\w)\2";
      string input = "AA";
      Match match = Regex.Match(input, pattern);
      
      // Get the first named group.
      Group group1 = match.Groups[1];
      Console.WriteLine("Group 1 value: {0}", group1.Success ? group1.Value : "Empty");
      
      // Get the second named group.
      Group group2 = match.Groups[2];
      Console.WriteLine("Group 2 value: {0}", group2.Success ? group2.Value : "Empty");
      
      // Get a non-existent group.
      Group group3 = match.Groups[3];
      Console.WriteLine("Group 3 value: {0}", group3.Success ? group3.Value : "Empty");
   }
}
// The example displays the following output:
//       Group 1 value: Empty
//       Group 2 value: A
//       Group 3 value: Empty
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(\d+)*(\w)\2"
      Dim input As String = "AA"
      Dim match As Match = Regex.Match(input, pattern)
      
      ' Get the first named group.
      Dim group1 As Group = match.Groups.Item(1)
      Console.WriteLine("Group 1 value: {0}", If(group1.Success, group1.Value, "Empty"))
      
      ' Get the second named group.
      Dim group2 As Group = match.Groups.Item(2)
      Console.WriteLine("Group 2 value: {0}", If(group2.Success, group2.Value, "Empty"))
      
      ' Get a non-existent group.
      Dim group3 As Group = match.Groups.Item(3)
      Console.WriteLine("Group 3 value: {0}", If(group3.Success, group3.Value, "Empty"))
   End Sub
End Module
' The example displays the following output:
'       Group 1 value: Empty
'       Group 2 value: A
'       Group 3 value: Empty

備註

屬性 Item[Int32] 是 C#) 中的索引 (,或類別 Visual Basic) 的預設屬性 (GroupCollection 。 它可讓您使用 foreach C# 中的 語句或 For Each Visual Basic 中的 語句來列舉集合的成員。

您也可以使用這個屬性,依其索引編號擷取個別擷取的群組。 您可以藉由呼叫 實例 Regex.GetGroupNumbers 方法,擷取陣列,其中包含正則運算式中所有擷取群組的數目。 您也可以藉由呼叫 實例 Regex.GroupNumberFromName 方法,將具名擷取群組對應至其號碼。

您可以擷取 屬性的值 Count ,以判斷集合中的專案數目。 參數的有效值 groupnum 範圍從 0 到小於集合中的專案數目。

屬性 GroupCollection 傳回的物件 Match.Groups 一律至少有一個成員。 如果正則運算式引擎在特定輸入字串中找不到任何相符專案,集合中的單 Group 一物件會將其 Group.Success 屬性設定為 false ,且其 Group.Value 屬性設定為 String.Empty

如果 groupnum 不是集合成員的索引,或者如果 groupnum 是輸入字串中尚未相符之擷取群組的索引,則方法會 GroupGroup.Success 傳回屬性為 且 Group.Value 其屬性 falseString.Empty 的物件。

另請參閱

適用於

Item[String]

Source:
GroupCollection.cs
Source:
GroupCollection.cs
Source:
GroupCollection.cs

以字串索引啟用對集合成員的存取。

public:
 property System::Text::RegularExpressions::Group ^ default[System::String ^] { System::Text::RegularExpressions::Group ^ get(System::String ^ groupname); };
public System.Text.RegularExpressions.Group this[string groupname] { get; }
member this.Item(string) : System.Text.RegularExpressions.Group
Default Public ReadOnly Property Item(groupname As String) As Group

參數

groupname
String

擷取群組的名稱。

屬性值

groupname 指定的集合成員。

實作

範例

下列範例會定義包含兩個具名群組的正則運算式。 第一個群組 numbers 會擷取一或多個連續的數位。 第二個群組 letter 會比對單一字元。 因為正則運算式引擎會尋找群組所 numbers 定義的模式零或一次出現, numbers 所以即使相符專案成功,群組也不會一律存在。 接著,此範例說明當 屬性用來擷取不相符的群組、相符群組,以及正則運算式中未定義的群組時 Item[String] 的結果。 此範例會定義正則運算式模式 (?<numbers>\d+)*(?<letter>\w)\k<letter> ,其解譯方式如下表所示。

模式 描述
(?<numbers>\d+)* 比對一或多個出現十進位數。 將此命名為 numbers 擷取群組。 比對此模式為零或一次。
(?<letter>\w) 比對單字字元。 將此命名為 letter 擷取群組。
\k<letter> 比對擷取群組所擷取的 letter 字串。
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(?<numbers>\d+)*(?<letter>\w)\k<letter>";
      string input = "AA";
      Match match = Regex.Match(input, pattern);
      
      // Get the first named group.
      Group group1 = match.Groups["numbers"];
      Console.WriteLine("Group 'numbers' value: {0}", group1.Success ? group1.Value : "Empty");
      
      // Get the second named group.
      Group group2 = match.Groups["letter"];
      Console.WriteLine("Group 'letter' value: {0}", group2.Success ? group2.Value : "Empty");
      
      // Get a non-existent group.
      Group group3 = match.Groups["none"];
      Console.WriteLine("Group 'none' value: {0}", group3.Success ? group3.Value : "Empty");
   }
}
// The example displays the following output:
//       Group 'numbers' value: Empty
//       Group 'letter' value: A
//       Group 'none' value: Empty
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(?<numbers>\d+)*(?<letter>\w)\k<letter>"
      Dim input As String = "AA"
      Dim match As Match = Regex.Match(input, pattern)
      
      ' Get the first named group.
      Dim group1 As Group = match.Groups.Item("numbers")
      Console.WriteLine("Group 'numbers' value: {0}", If(group1.Success, group1.Value, "Empty"))
      
      ' Get the second named group.
      Dim group2 As Group = match.Groups.Item("letter")
      Console.WriteLine("Group 'letter' value: {0}", If(group2.Success, group2.Value, "Empty"))
      
      ' Get a non-existent group.
      Dim group3 As Group = match.Groups.Item("none")
      Console.WriteLine("Group 'none' value: {0}", If(group3.Success, group3.Value, "Empty"))
   End Sub
End Module
' The example displays the following output:
'       Group 'numbers' value: Empty
'       Group 'letter' value: A
'       Group 'none' value: Empty

備註

groupName可以是正則運算式中name>) 元素所 (?< 定義的擷取組名,或由群組建構所定義之擷取群組數目的字串表示。 如需正則運算式中群組的詳細資訊,請參閱 群組建構

您可以呼叫 Regex.GetGroupNames 方法來擷取 物件中 Regex 所有擷取的群組名稱。 您也可以呼叫 Regex.GroupNameFromNumber 方法,將正則運算式中擷取群組的數目對應至其名稱。 然後,陣列中的個別名稱可以傳遞至 Item[String] 屬性,以擷取擷取的字串。

如果 groupname 不是集合中的擷取組名,或者如果 groupname 是輸入字串中尚未相符的擷取組名,則方法會 GroupGroup.Success 傳回屬性為 且 Group.Value 其屬性 falseString.Empty 的物件。

另請參閱

適用於