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);