困扰了很久,东西都是错的。
题目名称就是:验证一个邮箱名是否正确。注意几点就是  空白,点的位置,@的位置。@后不能紧跟着“.”。最后验证无错后,再输出用户名。要求我们substring indexOf等指令。由于我是初学者,所以各位高手的正则那些方法就不要发了。谢谢大家。老师给我们上课的平台是。vs2005.(控制台应用程序)

解决方案 »

  1.   

    正则表达式是最简单的,
    ^ 匹配字符串开始,$ 匹配字符串结尾
    \w 匹配英文数字下划线
    + 出现一次或更多次
    * 出现0次或多次
    [] 是一个集合
    () 是一个分组
    \. 代表字符http://www.cnblogs.com/morningwang/archive/2008/10/17/833810.html
      

  2.   


     //方法一
            public void checkEmail(string S)
            {
                //一个合法的邮箱地址
                // 必须包含 “@”、“.” 符号 (1、. 和 @不能出现在首末位置 2、有且只有一个 @ . 符号 3、.的后面必须是com 或者 cn)
                
                int a = 0, b = 0; //用来判断@ . 有多少个
                for (int i = 0; i < S.Length; i++)
                {
                    if (S == [email=]'@'[/email])
                    {
                        a++;
                    }
                    if (S == '.')
                    {
                        b++;
                    }
                   
                }
                //有且只有一个 @ . 符号时,继续进入判断
                if (a == 1 && b == 1)
                {
                    //. 和 @不能出现在首末位置
                    if (S[0] != [email=]'@'[/email] || S[0] != '.' || S[(S.Length - 1)] != [email=]'@'[/email] || S[(S.Length - 1)] != '.')
                    {
                        //取.后的字符串是否为 com 或者 cn
                        //int index = S.LastIndexOf(".");
                        //分割字符串,放到数组
                        string [] las = S.Split('.');
                      
                        //string last = "" ;
                        //for (int m = index + 1; m < S.Length; m++)
                        //{
                        //    last += S[m];
                        //}
                            
                        if (las[1] == "com" || las[1] == "cn")
                        {
                            Console.WriteLine(S + "邮箱地址合法!^^^^^^呵呵^^^^^");
                        }
                        else {
                            Console.WriteLine(S + "邮箱地址不合法!");
                        }
                        
                    }
                    else {
                        Console.WriteLine(S + "邮箱地址不合法!");
                    }
                }
                else {
                    Console.WriteLine(S + "邮箱地址不合法!");
                }
            }
            //方法二
            //用正则表达式验证邮箱
            public void check(string s)
            {
                if (Regex.IsMatch(s, @"^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
                {
                    Console.WriteLine(s + "邮箱地址合法!^^^^^^呵呵^^^^");
                }
                else {
                    Console.WriteLine(s + "邮箱地址不合法!");
                }
            }
      

  3.   

    //验证邮箱正则
    function CheckMail(obj) {
        if (obj.length > 0 && !obj.match(/^.+@.+$/)) {
            return false;
        }
        else {
            return true;
        }
    }
      

  4.   


            #region 电子邮件判断
            /// <summary>
            /// 名称:CheckEmail
            /// 功能:判断是否正确的电子邮件
            /// </summary>
            /// <param name="inputEmail">所要判断的电子邮件号</param>
            /// <returns>true,false</returns>
            public static bool CheckEmail(string inputEmail)
            {
                string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
                Regex re = new Regex(strRegex);
                if (re.IsMatch(inputEmail))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            #endregion
      

  5.   

    function ismail(mail) {
     var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
     if (filter.test(mail)) return true;
     else {
     alert('您的电子邮件格式不正确');
     return false;}
    }
      

  6.   

    根据你的问题随便写了一个,不是很好。 仅供参考!
    class Program {
            static void Main(string[] args) {
                try {
                  string s="";
                  Console.WriteLine("请输入一个邮箱地址:");
                  string mail=Console.ReadLine();
                  s=mail.Trim();//去掉所有的空格
                    int para1=s.IndexOf('@');//@的位置
                    int para2=s.IndexOf('.');//.的位置  
                    int m=s.Substring(0, para1).IndexOf('.');
                  bool rule1 =para2-para1>1?true:false;//.在@后边并且不是紧跟着
                    bool rule2=para2<s.Length?true:false;//.后边还有字符
                    bool rule3= para1>0?true:false;//.不是第一位
                    bool rule4=(m>0&&m<para1?false:true);//@前边的用户名没有.                if(rule1&&rule2&&rule3&&rule4) {
                        Console.WriteLine("验证通过!");
                        Console.ReadKey();
                    } else {
                        Console.WriteLine("邮箱不合规则!");
                        Console.ReadKey();
                                        }
                } catch(Exception e) {
                    Console.WriteLine("邮箱不合规则!");
                    Console.ReadKey();
                   
                }
            }
        }
      

  7.   

    注意要求,只能用substring   indexof
      

  8.   

    老师是在考查他们对于substring indexof的使用好不好?你以为老师会不知道正则表达式呀?看他的说话像是大一大二的,所以还没学。