
什么是正则表达式,在本篇不再赘述。
https://github.com/ziishaned/learn-regex
推荐上述地址,个人认为是一篇很好的介绍和学习正则表达式的文章,简练清楚。
2. C#中的正则表达式不严格的说,大概涉及到四个类:
需要说明的是:
接下来依次介绍
2.1 Regex类官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex
如下,传入一个string参数,正常new对象就可以(这是一个匹配邮箱的模式,形如 xxx@xxx.com)
string pattern = @"w+@w+.com"; Regex r = new Regex(pattern);
需要注意的是:
常用方法:
string pattern = @"w+@w+.com"; Regex r = new Regex(pattern); string text = "lai_yizhou@163.com; yizhou_lai@qq.com"; Match match = r.Match(text);
关于方法,值得一提的是:类的静态方法和实例方法,在功能层面上几乎等效。(使用类的静态方法不需要new对象)
string pattern = @"w+@w+.com"; Regex r = new Regex(pattern); string text = "lai_yizhou@163.com; yizhou_lai@qq.com"; // 使用new出来的对象的实例方法 Match match1 = r.Match(text); // 使用Regex类的静态方法 Match match2 = Regex.Match(text, pattern);2.2 Match类
官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.match
是一个描述匹配信息的类
需要注意的是:
string pattern = @"w+@w+.com";
Regex r = new Regex(pattern);
string text = "lai_yizhou@163.com; yizhou_lai@qq.com";
Match match = r.Match(text);
if (match.Success)
{
// Match Successfully
}
官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.matchcollection
需要注意的:
官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.group
string text = "lai_yizhou@163.com; yizhou_lai@qq.com";
按照前面这个例子,说明一次匹配可以产生多个Match对象。那么同理,一个Match对象里可以包含多个Group对象。
只需要在构建Regex类时,传入的字符串带一些括号,就可以把匹配到的子字符串分组(既然叫分组,那么这个类取名叫Group就比较合情合理了),便于匹配到了再取出来。
// 传入的字符串跟之前比,加了括号
string pattern = @"(w+)@(w+).com";
Regex r = new Regex(pattern);
string text = "lai_yizhou@163.com; yizhou_lai@qq.com";
Match match = r.Match(text);
if (match.Success)
{
// Match Successfully
}
根据前文描述,match 是匹配到了第一个(即 lai_yizhou@163.com),那么,“第一个” Group对象就是针对 lai_yizhou,“第二个” Group对象就是针对 163 的。
那么,怎么获取到这个Group对象?
请继续阅读。
2.5 GroupCollection类官方文档:https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.capturecollection
前面说到:Match对象 有一个属性 .Groups ,是一个 GroupCollection类 的对象
那么,针对这个 match对象
Match match = ... ; GroupCollection gCollect = match.Groups;
拿到了这个 GroupCollection类 的对象,遍历也好,下标访问也好,就可以拿到 Group类 的对象了。
需要注意的是:
按照下面的例子:
string pattern = @"(w+)@(w+).com"; Regex r = new Regex(pattern); string text = "lai_yizhou@163.com; yizhou_lai@qq.com"; MatchCollection matchCollection = r.Matches(text);
虽然加了两个括号,但是 matchCollection.Count == 3
如果用下标访问,matchCollection[0] 是整个字符串,matchCollection[1] 是描述 lai_yizhou,matchCollection[2] 是描述 163 的。
(这也就是为什么在前文描述的时候,“第一个” “第二个” 这两个词笔者打了引号)
2.6 Capture类 / CaptureCollection 类这部分本文不再赘述,推荐如下:
代码如下:
string pattern = @"(w+)@(w+).com";
Regex r = new Regex(pattern);
string text = "lai_yizhou@163.com; yizhou_lai@qq.com";
MatchCollection matchCollection = r.Matches(text);
int matchCount = matchCollection.Count;
if (matchCount != 0)
{
Console.WriteLine("matchCount = " + matchCount);
for (int matchIndex = 0; matchIndex < matchCount; ++matchIndex)
{
Console.WriteLine("tmatchIndex = " + matchIndex);
Match match = matchCollection[matchIndex];
Console.WriteLine("tmatchValue = " + match.Value);
int groupCount = match.Groups.Count;
Console.WriteLine("tmatchGroupCount = " + groupCount);
for (int groupIndex = 0; groupIndex < groupCount; groupIndex++)
{
Group group = match.Groups[groupIndex];
Console.WriteLine("ttgroupIndex = " + groupIndex);
Console.WriteLine("ttgroupValue = " + group.Value);
}
}
}
输出如下: