GroupCollection.Item[String] GroupCollection.Item[String] GroupCollection.Item[String] GroupCollection.Item[String] Property

定义

重载

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

允许通过整数索引访问集合成员。Enables access to a member of the collection by integer index.

Item[String] Item[String] Item[String] Item[String]

允许通过字符串索引访问集合成员。Enables access to a member of the collection by string index.

Item[Int32] Item[Int32] Item[Int32] 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 Int32 Int32 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

注解

属性是类的索引(在C#中为)或默认属性(在 Visual Basic 中)。 GroupCollection Item[Int32]The Item[Int32] property is the index (in C#) or the default property (in Visual Basic) of the GroupCollection class. 它允许使用中foreach C#的语句或For Each Visual Basic 中的语句枚举集合的成员。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到集合中的项数减1之间的值。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对象,其Group.Success属性为false ,其Group.Value属性为String.EmptyIf 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] Item[String] Item[String] 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 String String 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可以是正则表达式中的(?< name >)元素定义的捕获组的名称,也可以是由分组构造定义的捕获组数的字符串表示形式。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 Regex.GetGroupNames调用方法来检索对象中所有捕获组的名称。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方法将返回一个Group对象,该对象的属性为false,其Group.Value属性为String.EmptyIf 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.

另请参阅

适用于