Fork me on GitHub

正则表达式类

System.Text.RegularExpressions 命名空间提供正则表达式功能,主要包括7个类。他们的关系如右图,对于他们的功能很多书和网上资料写的很复杂,其实理一理还是很简单的。我分了四部分来定义:

 定义正则表达式:
Regex类
返回匹配结果:
MatchCollection类 和 Match类
返回捕获组的结果:
GropCollection类 和 Group类
返回执行捕获的结果结果:
CaptureCollection类 和 Captrue类
同时后三个相对的都是集合与子集的关系了。

 


 

Regex类的方法和属性都很简单,主要是对构造函数的运用不同,也不容易混淆,就不多言了。

Match类 可以使用Regex类的Match方法返回一个Match类对象。此时返回的时输入字符串的第一个匹配项,同时好多书中也没有提到NextMatch,而使很多人都以为使用该类的对象只能匹配第一项,其实可以遍历所有的匹配项。先前也,没查什么资料自己试着便利了一下,刚想起来看一下官方资料,一看和我的思路还是一样的,只不过我还想了一下,要是直接看的话还不用花时间想。


 

usingSystem;
usingSystem.Text.RegularExpressions;
namespacetMatch
{
classProgram
{
publicstaticvoidMain(string[]args)
{
string s="123abc456abcdabc123abc";
Regexr=newRegex("abc");
Matchm=r.Match(s);
inti=1;
while(m.Success)
{
Console.WriteLine("第{0}个匹配项,匹配值{1}-匹配索引{2}",i,m.Value,m.Index);
m=m.NextMatch();
++i;
}
Console.ReadKey(true);
}
}
}

 


MatchCollection表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合。

通过通过对 Regex.Matches 方法的特定调用获得。 在访问集合的 Count 属性(这个属性在Match类中是没有的)时,可使用该技术。 该技术填充集合的方法通常开销较大,会对性能造成较大的影响。那面上面的就可以简化了的使用遍历 Regex.Matches返回的对象了。

usingSystem;
usingSystem.Text.RegularExpressions;
namespacetMatchCollection
{
classProgram
{
publicstaticvoidMain(string[]args)
{
string s="123abc456abcdabc123abc";
MatchCollectionmc=Regex.Matches(s,"abc");
//遍历匹配项
for(inti=0;i<mc.Count;i++)
{
Console.WriteLine("第{0}个匹配项,匹配值{1}-匹配索引{2}",i,mc[i].Value,mc[i].Index);
}
Console.ReadKey(true);
}

}
}

静态 Matches 方法等效于使用指定的正则表达式模式构造 Regex 对象并调用实例方法 Matches。等效于:
Match match = regex.Match(input, startAt);
while (match.Success) 
 { 
// Handle match here...
 match = match.NextMatch();
 } 

 Group类和GropCollection类 都是为了返回单个的捕获组或者捕获组的集合类,所以你首先知道什么是捕获组,否则会滥用这个类,Match类的Groups方法获取由正则表达式匹配的组的集合。也就是返回GroupCollection类的实例。例如:

 

using System;
using System.Text.RegularExpressions;
namespace tMatchCollection
{
class Program
{
public static void Main(string[] args)
{
string input="123abc456abcdabc123abc";
string pattern="(a(b))c"; //此处定义了三个捕获组
Match m=Regex.Match(input,pattern);
GroupCollection gc=m.Groups;//获取由正则表达式匹配的组的集合
int n=1;
while(m.Success)
{
Console.WriteLine("第{0}个匹配项,匹配值{1}-匹配索引{2}",n+1,m.Value,m.Index);
m=m.NextMatch();
++n;
}
Console.WriteLine("捕获到的组数:{0}",gc.Count);
//我们来查看包含哪些捕获组
for(int i=0;i<gc.Count;i++)
{
Console.WriteLine("{0}:组{1}-起始索引{2}",i+1,gc[i].Value,gc[i].Index);
}
Console.ReadKey(true);
}
}
}

 


这里我就不再赘述Group类了,因为上面的例子里已经隐性的使用了Group类的实例。Group类的实例是由Match.Groups(groupname)属性返回的,或者是在使用(?<groupname>...)分组构造时,Match.Groups("groupname")属性返回的。


CaptureCollection类表示一个捕获组做出的捕获的集合,简单说也就是执行一个捕获组返回一个或多给字符串。
注意的一点就是它返回的是单个捕获组执行的捕获结果。例如:



using System;
using System.Text.RegularExpressions;
namespace test
{
class Program
{
public static void Main(string[] args)
{
string input="XYZAbcAbcAbcXYZAbcAb";
string pattern="(Abc)+";
const string posinfo="开始于第{0}个字符";
Match m=Regex.Match(input,pattern);
GroupCollection gc=m.Groups;//获取由正则表达式匹配的组的集合
Console.WriteLine("包含{0}个捕获组\n",gc.Count);
//我们来查看包含哪些捕获组
for(int i=0;i<gc.Count;i++)
{
CaptureCollection cc=gc[i].Captures;//返回单个捕获组执行的捕获结果
int Cnum=cc.Count;//每一组执行捕获的总数量
Console.WriteLine("{0}组执行捕获的数量为:{1}",gc[i].Value,cc.Count);
//现在我们查看执行捕获的具体信息
for(int ii=0;ii<Cnum;ii++)
{
Console.WriteLine(cc[ii].Value+posinfo,cc[ii].Index);
}
Console.WriteLine();
}
Console.ReadKey(true);
}
}
}


同上也不再赘述Capture类,例子里也已经隐性的使用了Capture类的实例。


 
 
 
 
posted @ 2011-11-19 22:38  Halower  阅读(1222)  评论(0编辑  收藏  举报