input正则邮箱_用正则表达式匹配邮箱地址

该程序展示了如何使用C#的正则表达式从多个网页中抓取邮箱地址。通过WebClient下载网页内容,然后使用正则模式匹配邮箱格式,包括标准格式、#号替换@、at替换@的表达式。最后,将找到的邮箱地址存储到列表并打印。
摘要由CSDN通过智能技术生成

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Net;

using System.IO;

namespace _07正则_匹配邮箱

{

class Program

{

static void Main(string[] args)

{

List listUrl = new List() {

new Uri("http://gb.corp.163.com/gb/contactus.html"),new Uri("https://passport.csdn.net/help/faq"),new Uri("http://www.kuaipan.cn/"),new Uri("http://www.ksyun.com/home/joinUs/campus"),new Uri("http://www.cnblogs.com/about/ad.aspx"),new Uri("http://www.cnblogs.com/about/contactus.aspx"),new Uri("http://www.csdn.net/company/statement.html"),new Uri("http://hb.qq.com/job/dczp/index.htm")

};

List listMail = new List();

foreach (Uri ur in listUrl)

{

GetMails(ur,listMail);

}

cw(listMail);

Console.ReadKey();

}

private static void GetMails(Uri uri,List list)

{

try

{

WebClient wc = new WebClient();

Console.WriteLine("创建WebClient - [{0}]",uri.ToString());

Stream stream = wc.OpenRead(uri);

//Console.WriteLine("正在下载:{0}",uri.ToString());

StreamReader reader = new StreamReader(stream,Encoding.Default);

string input = reader.ReadToEnd();

string reg = @"(?[a-zA-Z0-9_]+@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+)"

+ @"|((?[a-zA-Z0-9_]+#[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+))"

+ @"|((?[a-zA-Z0-9_]+\(at\)[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+))"

+ @"|((?[a-zA-Z0-9_]+@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+))";

Regex regex = new Regex(reg);

Console.WriteLine(Regex.IsMatch(input,reg));

MatchCollection matches = regex.Matches(input);

for (int i = 0; i < matches.Count; i++)

{

Match match = matches[i];

//Console.WriteLine("match: {0}",match.Value);

//Console.WriteLine(match.Groups.Count);

for (int j = 1; j < match.Groups.Count; j++)

{

string mail = match.Groups[j].Value;

if (!string.IsNullOrEmpty(mail))

{

mail = Regex.Replace(mail,@"(.+)(?:@)(.+)","$1@$2");

mail = Regex.Replace(mail,"(.+)#(.+)",@"(.+)\(at\)(.+)","$1@$2");

if (!list.Contains(mail))

{

list.Add(mail);

}

}

//Console.WriteLine("group: {0}",match.Groups[j].Value);

}

}

}

catch (Exception e)

{

Console.WriteLine(e.Message);

}

}

static void cw(List list)

{

Console.WriteLine("长度为{0}",list.Count);

int i = 0;

foreach (string str in list)

{

i++;

Console.WriteLine("{0} - [{1}]",i,str);

}

Console.WriteLine("______________________");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值