C#正则表达式GroupCollection类是什么呢?C#正则表达式GroupCollection类是如何使用的呢?下面让我们来具体的内容:
下面通过介绍 .NET 框架的正则表达式类,熟悉一下.NET框架下的正则表达式的使用方法。
C#正则表达式GroupCollection类表示捕获的组的集合
该集合为只读的,并且没有公共构造函数。GroupCollection 的实例在 Match.Groups 属性返回的集合中返回。下面的控制台应用程序查找并输出由正则表达式捕获的组的数目。
C#正则表达式GroupCollection类实例应用:
using System;
using System.Text.RegularExpressions;
public class RegexTest
{
public static void RunTest()
{
Regex r = new Regex("(a(b))c");
//定义组
Match m = r.Match("abdabc");
Console.WriteLine(
"Number of groups found = " + m.Groups.Count);
}
public static void Main()
{
RunTest();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
该示例产生下面的输出:
Number of groups found = 3
- 1.
C#正则表达式GroupCollection类的基本内容就向你介绍到这里,希望对你了解和学习C#正则表达式GroupCollection类有所帮助。
【编辑推荐】