您的位置:首页 > 编程语言 > C#

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

2015-09-11 11:48 369 查看
//正则表达式匹配邮箱
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: