有一个字符串address的值为
"张三" <[email protected]>; "ZhangYue" <[email protected]>; "李四" <[email protected]>; "小二" <[email protected]>
有没有什么比较简单的方法,将这个字符串地址清单解析生成MailAddressCollection

解决方案 »

  1.   

    正则表达式解析就可以了
    Mathes出来的是个集合
      

  2.   


                string str = @"""张三"" <[email protected]>; ""ZhangYue"" <[email protected]>; ""李四"" <[email protected]>; ""小二"" <[email protected]>";
                string strmatch = @"""([^""]+)""[^<>]+<([^<>]+)>";
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(strmatch);
                System.Text.RegularExpressions.MatchCollection mc = reg.Matches(str);
                foreach (System.Text.RegularExpressions.Match mm in mc)
                {
                    MessageBox.Show(mm.Groups[1].Value);
                    MessageBox.Show(mm.Groups[2].Value);
                }
      

  3.   

    string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt                MailAddressCollection mac = new MailAddressCollection();
                   var _list = Regex.Matches(tempStr, @"(?i)(['""]?)(?<Name>[^'""]*?)\1\s+?\<(?<Adress>[^<>;]+)\>").Cast<Match>().Select(a => new MailAddress(a.Groups["Adress"].Value,a.Groups["Name"].Value));
                   _list.ToList().ForEach(x=>mac.Add(x));
      

  4.   

    谢谢大家,我还以为MailAddressCollection有什么方法可以直接加载这样的字符串生成相应的对象呢