Best way to parse string of email addresses

后端 未结 13 2616
悲哀的现实
悲哀的现实 2021-02-14 04:10

So i am working with some email header data, and for the to:, from:, cc:, and bcc: fields the email address(es) can be expressed in a number of different ways:

F         


        
13条回答
  •  说谎
    说谎 (楼主)
    2021-02-14 05:03

    // Based on Michael Perry's answer * // needs to handle first.last@domain.com, first_last@domain.com and related syntaxes // also looks for first and last name within those email syntaxes

    public class ParsedEmail
    {
        private string _first;
        private string _last;
        private string _name;
        private string _domain;
    
        public ParsedEmail(string first, string last, string name, string domain)
        {
            _name = name;
            _domain = domain;
    
            // first.last@domain.com, first_last@domain.com etc. syntax
            char[] chars = { '.', '_', '+', '-' };
            var pos = _name.IndexOfAny(chars);
    
            if (string.IsNullOrWhiteSpace(_first) && string.IsNullOrWhiteSpace(_last) && pos > -1)
            {
                _first = _name.Substring(0, pos);
                _last = _name.Substring(pos+1);
            }
        }
    
        public string First
        {
            get { return _first; }
        }
    
        public string Last
        {
            get { return _last; }
        }
    
        public string Name
        {
            get { return _name; }
        }
    
        public string Domain
        {
            get { return _domain; }
        }
    
        public string Email
        {
            get
            {
                return Name + "@" + Domain;
            }
        }
    
        public override string ToString()
        {
            return Email;
        }
    
        public static IEnumerable SplitEmailList(string delimList)
        {
            delimList = delimList.Replace("\"", string.Empty);
    
            Regex re = new Regex(
                        @"((?\w*), (?\w*) <(?[a-zA-Z_0-9\.\+\-]+)@(?\w*\.\w*)>)|" +
                        @"((?\w*) (?\w*) <(?[a-zA-Z_0-9\.\+\-]+)@(?\w*\.\w*)>)|" +
                        @"((?[a-zA-Z_0-9\.\+\-]+)@(?\w*\.\w*))");
    
    
            MatchCollection matches = re.Matches(delimList);
    
            var parsedEmails =
                       (from Match match in matches
                        select new ParsedEmail(
                                match.Groups["first"].Value,
                                match.Groups["last"].Value,
                                match.Groups["name"].Value,
                                match.Groups["domain"].Value)).ToList();
    
            return parsedEmails;
    
        }
    
    
    }
    

提交回复
热议问题