Regex rx = new Regex(@"[A-Z0-9a-z\.\-_]+@([a-z0-9]+)\.[a-z0-9A-z]");
string html = File.ReadAllText("1.htm");
//提取Email,
//通过()提取组。
//fd(s(a(fds)a)f)sf(d(s(af)d)s)a
MatchCollection mc = rx.Matches(html, 0);
//请统计出常用邮件服务提供商的用户使用。
//163
//sohu
//gmail
//qq
int count_163 = 0;
int count_gmail = 0;
int count_qq = 0;
foreach (Match match in mc)
{
//通过match.Groups[]来获取提取组。注意:第0组存储的是完整匹配字符串,要获取组因该从索引1开始。
switch (match.Groups[1].Value.ToLower())
{
case "163":
count_163++;
break;
case "gmail":
count_gmail++;
break;
case "qq":
count_qq++;
break;
}
}
Console.WriteLine(mc.Count);
Console.WriteLine("网易邮箱用户数:{0}", count_163);
Console.WriteLine("gmail邮箱用户数:{0}", count_gmail);
Console.WriteLine("qq邮箱用户数:{0}", count_qq);
分组替换
string msg = "234--234--------------34-55";
Regex remsg=new Regex(@"(\-)+");
msg=remsg.Replace(msg,"-");
Console.WriteLine(msg);
string msg1 = "我的生日是05/21/2010耶";
Regex remsg1 = new Regex(@"(\d{1,2})\/(\d{1,2})\/(\d{4})");
msg1 = remsg1.Replace(msg1, "$3-$2-$1");
Console.WriteLine(msg1);