C#正则表达式校验
static void Main(string[] args) { WebClient wc = new WebClient(); //要把网页以字符串的形式下载下来 string htmlstr = wc.DownloadString("http://zhidao.baidu.com/link?url=6kcGgrcqb84u-4NDe7aoUSCdmHVt-2DbKffrD5DiwZodgalkfPbnbhD1Hy2v_By4k2IN6tYtFt59Qwor7HRLpHflaaHnoXGYR9dhGX1r55K"); MatchCollection mc = Regex.Matches(htmlstr, "[0-9a-zA-Z_.-]+@[0-9a-zA-Z_.]+([.][a-zA-Z]+){1,2}"); foreach (Match item in mc) { if (item.Success)//是否匹配成功,写上更好 { Console.WriteLine(item.Value); } } Console.ReadKey(); }
static void Main(string[] args) { string eamil = Console.ReadLine(); //是否匹配 bool result = Regex.IsMatch(eamil, "[0-9a-zA-Z_.-]+@[0-9a-zA-Z_.]+([.][a-zA-Z]+){1,2}"); //获取匹配的结果 Match mt = Regex.Match(eamil, "[0-9a-zA-Z_.-]+@[0-9a-zA-Z_.]+([.][a-zA-Z]+){1,2}"); Console.WriteLine(result); Console.WriteLine(mt.Value); Console.ReadKey(); }
获取网页的图片:
using System; using System.Net; using System.Text; using System.Text.RegularExpressions; namespace GetPictures { class Program { static void Main(string[] args) { //首先下载页面 WebClient wc = new WebClient(); wc.Encoding = Encoding.UTF8; string html = wc.DownloadString("https://www.camarts.cn/archives/3020.html"); MatchCollection mc = Regex.Matches(html, "<img\\sclass=\"aligncenter\\ssize-full\\swp-image-[0-9]{4}?\"\\ssrc=\"(.+?)\".+?>"); //遍历匹配的内容 int i=0; foreach (Match item in mc) { i++; if (item.Success) { string path = item.Groups[1].Value;//图片下载地址 //下载图片 if(i%2==0) { wc.DownloadFile(path, @"D:\高清图片\" + i + ".jpg"); Console.WriteLine("★"+i/2);//进度 } /* <img class="aligncenter size-full wp-image-3021" src="https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9659.jpg?imageView/2/w/800/h/800/q/90" data-original="https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9659.jpg?imageView/2/w/800/h/800/q/90" alt="IMG_9659" width="800" height="533" srcset="https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9659.jpg 800w, https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9659-150x99.jpg 150w, https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9659-300x199.jpg 300w, https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9659-450x300.jpg 450w" sizes="(max-width: 800px) 100vw, 800px" style="display: block;"> <img class="aligncenter size-full wp-image-3022" src="https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9668.jpg?imageView/2/w/800/h/800/q/90" data-original="https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9668.jpg?imageView/2/w/800/h/800/q/90" alt="IMG_9668" width="800" height="533" srcset="https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9668.jpg 800w, https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9668-150x99.jpg 150w, https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9668-300x199.jpg 300w, https://dn-ssl-img-camarts.qbox.me/2014/07/IMG_9668-450x300.jpg 450w" sizes="(max-width: 800px) 100vw, 800px" style="display: block;"> <img\\sclass=\"aligncenter\\ssize-full\\swp-image-[1-9]+\"\\ssrc=\"(.+.+?)\".+?> */ } } Console.ReadKey(); } } }