1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > input正则邮箱_用正则表达式匹配邮箱地址

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

时间:2018-08-12 11:59:06

相关推荐

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using ;

using System.IO;

namespace _07正则_匹配邮箱

{

class Program

{

static void Main(string[] args)

{

List listUrl = new List() {

new Uri("http://gb./gb/contactus.html"),new Uri("/help/faq"),new Uri("/"),new Uri("/home/joinUs/campus"),new Uri("/about/ad.aspx"),new Uri("/about/contactus.aspx"),new Uri("/company/statement.html"),new Uri("/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("______________________");

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。