Best way to parse string of email addresses

后端 未结 13 2607
悲哀的现实
悲哀的现实 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:01

    Your 2nd email example is not a valid address as it contains a comma which is not within a quoted string. To be valid it should be like: "Last, First".

    As for parsing, if you want something that is quite strict, you could use System.Net.Mail.MailAddressCollection.

    If you just want to your input split into separate email strings, then the following code should work. It is not very strict but will handle commas within quoted strings and throw an exception if the input contains an unclosed quote.

    public List SplitAddresses(string addresses)
    {
        var result = new List();
    
        var startIndex = 0;
        var currentIndex = 0;
        var inQuotedString = false;
    
        while (currentIndex < addresses.Length)
        {
            if (addresses[currentIndex] == QUOTE)
            {
                inQuotedString = !inQuotedString;
            }
            // Split if a comma is found, unless inside a quoted string
            else if (addresses[currentIndex] == COMMA && !inQuotedString)
            {
                var address = GetAndCleanSubstring(addresses, startIndex, currentIndex);
                if (address.Length > 0)
                {
                    result.Add(address);
                }
                startIndex = currentIndex + 1;
            }
            currentIndex++;
        }
    
        if (currentIndex > startIndex)
        {
            var address = GetAndCleanSubstring(addresses, startIndex, currentIndex);
            if (address.Length > 0)
            {
                result.Add(address);
            }
        }
    
        if (inQuotedString)
            throw new FormatException("Unclosed quote in email addresses");
    
        return result;
    }
    
    private string GetAndCleanSubstring(string addresses, int startIndex, int currentIndex)
    {
        var address = addresses.Substring(startIndex, currentIndex - startIndex);
        address = address.Trim();
        return address;
    }
    

提交回复
热议问题