解析电子邮件地址的字符串的最佳方式 [英] Best way to parse string of email addresses
问题描述
所以,我正在与一些电子邮件标题的数据,并为从,抄送:和密件抄送::字段中的电子邮件地址(ES)可以pssed在许多不同的方式EX $ P $
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:
First Last <name@domain.com>
Last, First <name@domain.com>
name@domain.com
而这些变化可能出现在相同的消息,以任意顺序,都在同一个逗号分隔字符串:
And these variations can appear in the same message, in any order, all in one comma separated string:
First, Last <name@domain.com>, name@domain.com, First Last <name@domain.com>
我一直在试图想出一个方法来解析这个字符串转换成独立的姓,名,电子邮件的每个人(省略如果只有一个电子邮件地址提供的名称)。
I've been trying to come up with a way to parse this string into separate First Name, Last Name, E-Mail for each person (omitting the name if only an email address is provided).
有人建议可以做到这一点的最好方法是什么?
Can someone suggest the best way to do this?
我试图分裂逗号,这将工作,除了在姓氏首先放置第二个例子。我想这个方法可以工作,如果我分裂后,我检查每个元素,看看它是否包含'@'或'&LT;'/'>',如果它不那么它可以假设下一个元素第一个名字。这是处理这个好办法?我是否忽略了另一种格式的地址可能是?
I've tried to Split on the commas, which would work except in the second example where the last name is placed first. I suppose this method could work, if after i split, i examine each element and see if it contains a '@' or '<'/'>', if it doesn't then it could be assumed that the next element is the first name. Is this a good way to approach this? Have i overlooked another format the address could be in?
更新:也许我要澄清一点,基本上所有我希望做的是打破了包含多个地址到含有地址个体串以任何形式被发送的,我有我自己的方法来验证字符串,提取的地址信息,这只是棘手我找出分隔每个地址的最佳方式。
UPDATE: Perhaps i should clarify a little, basically all i am looking to do is break up the string containing the multiple addresses into individual strings containing the address in whatever format it was sent in. I have my own methods for validating and extracting the information from an address, it was just tricky for me to figure out the best way to separate each address.
下面是解决方案,我想出了要做到这一点:
Here is the solution i came up with to accomplish this:
String str = "Last, First <name@domain.com>, name@domain.com, First Last <name@domain.com>, \"First Last\" <name@domain.com>";
List<string> addresses = new List<string>();
int atIdx = 0;
int commaIdx = 0;
int lastComma = 0;
for (int c = 0; c < str.Length; c++)
{
if (str[c] == '@')
atIdx = c;
if (str[c] == ',')
commaIdx = c;
if (commaIdx > atIdx && atIdx > 0)
{
string temp = str.Substring(lastComma, commaIdx - lastComma);
addresses.Add(temp);
lastComma = commaIdx;
atIdx = commaIdx;
}
if (c == str.Length -1)
{
string temp = str.Substring(lastComma, str.Legth - lastComma);
addresses.Add(temp);
}
}
if (commaIdx < 2)
{
// if we get here we can assume either there was no comma, or there was only one comma as part of the last, first combo
addresses.Add(str);
}
以上code生成单独的地址,我可以向下行进一步处理。
The above code generates the individual addresses that i can process further down the line.
推荐答案
下面是解决方案,我想出了要做到这一点:
Here is the solution i came up with to accomplish this:
String str = "Last, First <name@domain.com>, name@domain.com, First Last <name@domain.com>, \"First Last\" <name@domain.com>";
List<string> addresses = new List<string>();
int atIdx = 0;
int commaIdx = 0;
int lastComma = 0;
for (int c = 0; c < str.Length; c++)
{
if (str[c] == '@')
atIdx = c;
if (str[c] == ',')
commaIdx = c;
if (commaIdx > atIdx && atIdx > 0)
{
string temp = str.Substring(lastComma, commaIdx - lastComma);
addresses.Add(temp);
lastComma = commaIdx;
atIdx = commaIdx;
}
if (c == str.Length -1)
{
string temp = str.Substring(lastComma, str.Legth - lastComma);
addresses.Add(temp);
}
}
if (commaIdx < 2)
{
// if we get here we can assume either there was no comma, or there was only one comma as part of the last, first combo
addresses.Add(str);
}
这篇关于解析电子邮件地址的字符串的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!