Regex for validating multiple E-Mail-Addresses

后端 未结 10 1632
忘了有多久
忘了有多久 2020-12-01 00:32

I got a Regex that validates my mail-addresses like this:

([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]

相关标签:
10条回答
  • 2020-12-01 01:03

    In an old regex book they stated that you cannot write a regex to match all valid email addresses (although you can come close).

    Here is a website dealing with regex and email addresses.

    I would recommend that you split the string at ; and , boundaries and check each email address separately for being valid/invalid with your regex.

    0 讨论(0)
  • 2020-12-01 01:04

    Here is a simple Program which does this for you without using the Regular Expression.Well Actually it does use a regular expression but we don't have to worry about it how it looks .php does this for us .

    public function test_reg()
    
    {
    
       $email_list = '';
    
            $array = explode(";",$email_list);
    
            foreach ($array as $value)
             {
                $value;
    
                if (!filter_var($value, FILTER_VALIDATE_EMAIL) === false)
                 {
                     $msg =  "Email List Contains  valid email addresses.";
                  } 
                else
                 {
                     $msg ="Your Email list contains an invalid email at. &nbsp&nbsp &nbsp".$value;
                     break;
                 }  
             }
    
                    echo $msg;
    
    
    
    }
    

    You have to separate your emails by a semi colon.

    0 讨论(0)
  • 2020-12-01 01:04

    This is a very old question, but I figured I'd share my C# code.

    I decided to parse by the semicolon then check each email individually:

    string toAddress = "testemail@gmail.com;test2@testing.com;";
    Regex rgx = new Regex(
        @"^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)*$");
    
    List<string> emailArray = new List<string>();
    
    if(toAddress != null && toAddress != "")
    {
        if (toAddress.IndexOf(";") != -1)
        {
            emailArray = toAddress.Replace(" ","").Split(';').ToList();
        }
        else
        {
            emailArray.Add(toAddress);
        }
    
        foreach (string email in emailArray)
        {
            if (rgx.IsMatch(email ?? ""))
            {
                SendEmail(email, subject, body);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 01:08

    Please try this

    ^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4};?)+$
    
    0 讨论(0)
提交回复
热议问题