GroupCollection.Item[String] 屬性

定義

啟用依據整數或字串索引來存取集合的單一成員。Enables access to a single member of the collection by integer or string index.

多載

Item[Int32]

以整數索引啟用對集合成員的存取。Enables access to a member of the collection by integer index.

Item[String]

以字串索引啟用對集合成員的存取。Enables access to a member of the collection by string index.

Item[Int32]

以整數索引啟用對集合成員的存取。Enables access to a member of the collection by integer index.

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

要擷取之集合成員的以零起始的索引。The zero-based index of the collection member to be retrieved.

屬性值

groupnum 指定的集合成員。The member of the collection specified by groupnum.

範例

下列範例會定義由兩個編號群組組成的正則運算式。The following example defines a regular expression that consists of two numbered groups. 第一個群組會捕捉一或多個連續數位。The first group captures one or more consecutive digits. 第二個群組則符合單一字元。The second group matches a single character. 因為正則運算式引擎會尋找第一個群組的零或一次出現,所以即使正則運算式比對成功,也不一定會找到相符專案。Because the regular expression engine looks for zero or one occurrence of the first group, it does not always find a match even if the regular expression match is successful. 然後,此範例會說明當 Item[Int32] 屬性用來抓取不相符的群組、相符的群組,以及正則運算式中未定義的群組時的結果。The example then illustrates the result when the Item[Int32] property is used to retrieve an unmatched group, a matched group, and a group that is not defined in the regular expression. 此範例會定義正則運算式模式 (\d+)*(\w)\2,如下表所示加以解讀。The example defines a regular expression pattern (\d+)*(\w)\2, which is interpreted as shown in the following table.

模式Pattern 說明Description
(\d+)* 符合一或多個出現的十進位數。Match one or more occurrence of a decimal digit. 這是第一個擷取群組。This is the first capturing group. 比對此模式為零或一次。Match this pattern either zero or one time.
(\w) 這是第二個擷取群組。This is the second capturing group.
\k 符合第二個捕獲群組所捕獲的字串。Match the string captured by the second capturing group.
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] 屬性是 GroupCollection 類別的索引( C#在中)或預設屬性(在 Visual Basic 中)。The Item[Int32] property is the index (in C#) or the default property (in Visual Basic) of the GroupCollection class. 它可讓您使用中C#的 foreach 語句或 Visual Basic 中的 For Each 語句,列舉集合的成員。It allows you to enumerate the members of the collection by using a foreach statement in C# or a For Each statement in Visual Basic.

您也可以使用這個屬性,依索引編號來抓取個別的已捕捉群組。You can also use this property to retrieve individual captured groups by their index number. 您可以藉由呼叫實例 Regex.GetGroupNumbers 方法,來抓取包含正則運算式中所有捕捉群組數目的陣列。You can retrieve an array that contains the numbers of all capturing groups in a regular expression by calling the instance Regex.GetGroupNumbers method. 您也可以藉由呼叫實例 Regex.GroupNumberFromName 方法,將名稱為「捕捉群組」對應至其數位。You can also map named capturing groups to their numbers by calling the instance Regex.GroupNumberFromName method.

您可以藉由抓取 Count 屬性的值來判斷集合中的專案數。You can determine the number of items in the collection by retrieving the value of the Count property. groupnum 參數的有效值範圍是從0到小於集合中的專案數。Valid values for the groupnum parameter range from 0 to one less than the number of items in the collection.

Match.Groups 屬性所傳回的 GroupCollection 物件一律至少有一個成員。The GroupCollection object returned by the Match.Groups property always has at least one member. 如果正則運算式引擎在特定輸入字串中找不到任何相符專案,則集合中的單一 Group 物件會將其 Group.Success 屬性設定為 false 而且其 Group.Value 屬性會設定為 [String.Empty]。If the regular expression engine cannot find any matches in a particular input string, the single Group object in the collection has its Group.Success property set to false and its Group.Value property set to String.Empty.

如果 groupnum 不是集合成員的索引,或如果 groupnum 是未在輸入字串中比對之捕捉群組的索引,則此方法會傳回 Group.Success 屬性為 falseGroup 物件,而其 @no__String.Emptyt_5_ 屬性。Group.ValueIf groupnum is not the index of a member of the collection, or if groupnum is the index of a capturing group that has not been matched in the input string, the method returns a Group object whose Group.Success property is false and whose Group.Value property is String.Empty.

另請參閱

Item[String]

以字串索引啟用對集合成員的存取。Enables access to a member of the collection by string index.

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

擷取群組的名稱。The name of a capturing group.

屬性值

groupname 指定的集合成員。The member of the collection specified by groupname.

範例

下列範例會定義由兩個命名群組組成的正則運算式。The following example defines a regular expression that consists of two named groups. 第一個群組(numbers)會捕捉一或多個連續數位。The first group, numbers, captures one or more consecutive digits. 第二個群組(letter)符合單一字元。The second group, letter, matches a single character. 因為正則運算式引擎會尋找 numbers 群組所定義的零或一次模式,所以即使相符成功,numbers 群組也不一定會存在。Because the regular expression engine looks for zero or one occurrence of the pattern defined by the numbers group, the numbers group is not always present even if a match is successful. 然後,此範例會說明當 Item[String] 屬性用來抓取不相符的群組、相符的群組,以及正則運算式中未定義的群組時的結果。The example then illustrates the result when the Item[String] property is used to retrieve an unmatched group, a matched group, and a group that is not defined in the regular expression. 此範例會定義正則運算式模式 (?<numbers>\d+)*(?<letter>\w)\k<letter>,如下表所示加以解讀。The example defines a regular expression pattern (?<numbers>\d+)*(?<letter>\w)\k<letter>, which is interpreted as shown in the following table.

模式Pattern 說明Description
(?<numbers>\d+)* 符合一或多個出現的十進位數。Match one or more occurrence of a decimal digit. 將此命名為 numbers 的「捕獲群組」。Name this the numbers capturing group. 比對此模式為零或一次。Match this pattern either zero or one time.
(?<letter>\w) 符合單一文字字元。Match a single word character. 將此命名為 letter 的「捕獲群組」。Name this the letter capturing group.
\k<letter> 符合 letter 捕捉群組所捕捉的字串。Match the string captured by the letter capturing group.
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 可以是正則運算式中 (?<名稱>) 元素所定義的「捕捉」組名,或是群組結構所定義之「捕捉群組」數目的字串表示。groupName can be either the name of a capturing group that is defined by the (?<name>) element in a regular expression, or the string representation of the number of a capturing group that is defined by a grouping construct. 如需正則運算式中群組的詳細資訊,請參閱群組結構For more information about groups in regular expressions, see Grouping Constructs.

您可以藉由呼叫 Regex.GetGroupNames 方法,抓取 Regex 物件中所有已捕捉群組的名稱。You can retrieve the names of all the captured groups in a Regex object by calling the Regex.GetGroupNames method. 您也可以藉由呼叫 Regex.GroupNameFromNumber 方法,將正則運算式中的捕捉群組數目對應到其名稱。You can also map the numbers of capturing groups in a regular expression to their names by calling the Regex.GroupNameFromNumber method. 然後,可以將陣列中的個別名稱傳遞至 Item[String] 屬性,以抓取已捕捉的字串。Individual names from the array can then be passed to the Item[String] property to retrieve the captured string.

如果 groupname 不是集合中的捕捉群組名稱,或如果 groupname 是未在輸入字串中比對的「捕捉」群組名稱,此方法會傳回 Group.Success 屬性為 falseGroup 物件,而其String.EmptyGroup.Value 屬性。If groupname is not the name of a capturing group in the collection, or if groupname is the name of a capturing group that has not been matched in the input string, the method returns a Group object whose Group.Success property is false and whose Group.Value property is String.Empty.

另請參閱

適用於