码迷,mamicode.com
首页 > Windows程序 > 详细

C#高级------正则验证邮箱

时间:2015-09-11 12:15:27      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

//正则表达式匹配邮箱
            Console.WriteLine("请输入邮箱");
            string s = Console.ReadLine();

            bool b= Regex.IsMatch(s, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            if(!b)
            {
                Console.WriteLine("邮箱不合法");
            }
            else
            {
                Console.WriteLine("邮箱合法");
            }
            Console.ReadKey();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace out_ref
{
    class Program
    {
        static void Main(string[] args)
        {
           //下载页面所有邮箱信息
            WebClient wc = new WebClient();
            //下载网页
            string html = wc.DownloadString("http://bbs.tianya.cn/post-374-27866-1.shtml");
            //邮箱正则,拿到邮箱,MatchCollection是一个集合
            MatchCollection mc =  Regex.Matches(html, @"([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)");
            //遍历集合内容
            foreach (Match item in mc)
            {
                if (item.Success)//如果匹配成功
                {
                    Console.WriteLine(item.Value);
                }
            }
            Console.WriteLine(mc.Count);
            Console.ReadKey();
        }
    }
}

 

C#高级------正则验证邮箱

标签:

原文地址:http://www.cnblogs.com/phpweige/p/4800503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!