日期:2014-05-17 浏览次数:21155 次
while (a == -1) { Console.WriteLine("邮件地址格式不正确!"); Console.Write("请重新输入一个邮箱地址:"); email = Convert.ToString(Console.ReadLine()); a = email.IndexOf("@"); } string user = email.Substring(0, a); string yu = email.Substring(a + 1, email.Length - a - 1); if(yu==".com") Console.WriteLine("您所输入的邮箱的用户名为:{0},域名为:{1}",user,yu); Console.ReadKey();
------解决方案--------------------
用正则表达式:只要是拼写正则表达式:我看了下这正则是正确的。在用微软的类库:Regex.IsMatch做判断
代码大概如下:
C#版本
private void cmdConfirm_Click(object sender, System.EventArgs e)
{
string pattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
string strEmail = TextBox1.Text.Trim();
if( System.Text.RegularExpressions.Regex.IsMatch(strEmail ,pattern) )
{
Response.Write("<script>alert('正确!');</script>"); }
else
{
Response.Write("<script>alert('错误!');</script>"); }
}
VS2005中邮箱正则表达式
今天需要一个关邮电子邮箱输入的正则表达式,于是在控制台程序做了一个测试
static void Main(string[] args)
{
string emailPattern = @"^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$";
Console.Write("Enter an e-mail address:");
string emailInput = Console.ReadLine();
bool match = Regex.IsMatch(emailInput, emailPattern);
if (match)
{
Console.WriteLine("E-mail address is valid");
Console.ReadLine();
}
else
{
Console.WriteLine("Suppliied input is not a valid e-mail address");
Console.ReadLine();
}