用正则表达式匹配邮箱地址
2013-07-02 22:04
417 查看
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<Uri> listUrl = new List<Uri>() { 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<string> listMail = new List<string>(); foreach (Uri ur in listUrl) { GetMails(ur, listMail); } cw(listMail); Console.ReadKey(); } private static void GetMails(Uri uri,List<string> 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 = @"(?<mail1>[a-zA-Z0-9_]+@[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+)" + @"|((?<mail2>[a-zA-Z0-9_]+#[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+))" + @"|((?<mail2>[a-zA-Z0-9_]+\(at\)[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)+))" + @"|((?<mail2>[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, "(.+)#(.+)", "$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<string> list) { Console.WriteLine("长度为{0}", list.Count); int i = 0; foreach (string str in list) { i++; Console.WriteLine("{0} - [{1}]", i, str); } Console.WriteLine("______________________"); } } }
相关文章推荐
- Java代码:使用正则表达式匹配电子邮箱地址
- 抓取网页并用正则表达式匹配邮箱地址
- Python 正则表达式 匹配邮箱地址
- 正则表达式匹配MAC地址、邮箱地址、IP地址
- 正则表达式:网页爬虫:从TXT中获取邮箱地址(获取的练习,缺点:一行只能匹配一个)
- 正则表达式——从匹配北美电话号码和邮箱地址学习
- 匹配邮箱正则表达式
- 正则表达式匹配EXCEL地址字符串
- asp email邮箱地址验证正则表达式
- iphone匹配邮箱的正则表达式
- 用正则表达式找出一个文件中有多少个邮箱地址
- Python-同时匹配邮箱和电话号码的正则表达式
- 正则表达式匹配身份证 电话号码 邮箱 住宅电话 邮编等
- 【lua】Lua正则表达式匹配邮箱
- C#正则表达式匹配HTML中的图片路径,图片地址
- python正则表达式匹配邮箱
- 【Lua正则表达式】解析匹配优酷电视剧集的下载地址用来批量下载
- javascript 电话,QQ,手机,身份证号,邮箱地址,邮政 正则表达式验证代码
- Javascript正则表达式验证邮箱地址
- IP地址、手机号码、邮箱地址C#正则表达式