I am trying to take a list of eMail addresses along with first and last names and convert them to a CSV format. My eMail addresses are in the following format:
我试图获取电子邮件地址列表以及名字和姓氏,并将它们转换为CSV格式。我的电子邮件地址采用以下格式:
First, Last <email1@example.com>; First, Last <email2@example.com>;
The output I need is the following:
我需要的输出如下:
email1@example.com,email2@example.com
I am using the following code:
我使用以下代码:
string[] addresses = addresses_Delimited.Split(new Char[] { '<', '>' });
addresses_Delimited
is my list of addresses in the original format.
addresses_Delimited是我原始格式的地址列表。
The problem is that it is not eliminating first and last names; instead it is returning first and last names as entries in the array addresses
. So, addresses[0]
= "First, Last", addresses[1]
= "email1@example.com", and addresses[2]
= "; First, Last". All first and last name entries after the first one have a semicolon in them.
问题是它没有消除名字和姓氏;相反,它将返回名字和姓氏作为数组地址中的条目。因此,地址[0] =“第一个,最后一个”,地址[1] =“email1@example.com”,地址[2] =“;第一个,最后一个”。第一个之后的所有名字和姓氏条目都包含分号。
How do I make string.Split
remove all text outside "<" and ">"? Do I need to use something else?
如何使string.Split删除“<”和“>”之外的所有文本?我需要使用其他东西吗?
5 个解决方案
#1
Rather than using a Split
which does not care that the delimiters are paired up, use a regular expression like this:
而不是使用不关心分隔符配对的Split,使用这样的正则表达式:
<([^>]+)>
When you apply this regex to your input strings, you would capture the content of angular brackets into capturing group number 1:
将此正则表达式应用于输入字符串时,您将捕获角括号的内容到捕获组编号1:
var s = "First, Last <email1@example.com>; First, Last <email2@example.com>;";
Regex regex = new Regex(@"<([^>]+)>");
foreach (Match m in regex.Matches(s)) {
Console.WriteLine(m.Groups[1]);
}
#2
Split
won't work in this case. You need to use Regular Expressions. Try this
拆分在这种情况下不起作用。您需要使用正则表达式。试试这个
// using System.Text.RegularExpressions;
// pattern = any number of arbitrary characters between < and >.
var pattern = @"\<(.*?)\>";
var matches = Regex.Matches(addresses_Delimited, pattern);
foreach (Match m in matches) {
Console.WriteLine(m.Groups[1]);
}
#3
You can do it with split - but it's really ugly:
你可以用拆分来做 - 但它真的很难看:
var text = "First, Last <email1@example.com>; First, Last <email2@example.com>;";
var t = text.TrimEnd(';').Split(';');
foreach (var m in t)
{
Console.WriteLine(m.Split('<')[1].TrimEnd('>'));
}
Use RegularExpression instead.
请改用RegularExpression。
#4
Assuming (and this is a big assumption) that there are no ;
characters in any names or emails and that there are no ,
characters in any emails, this will work:
假设(这是一个很大的假设)没有;任何名称或电子邮件中的字符以及任何电子邮件中都没有字符,这将起作用:
using System.Linq;
using System.Net.Mail;
...
var input = "First, Last <email1@example.com>; First, Last <email2@example.com>;";
var emails = String.Join(",", input
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => new MailAddress(s).Address));
#5
Split by ";' first, then by "<" and ">".
拆分“;”首先,然后是“<”和“>”。
string inputEmails = "First1, Last1 <email1@example.com>; First2, Last2 <email2@example.com>;";
string[] inputEmailsArray = inputEmails.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string email in inputEmailsArray)
{
string[] inputEmailArray = email.Split(new char[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string emailPart in inputEmailArray)
{
string s = emailPart; // First1, Last1 // email1@example.com
}
}
#1
Rather than using a Split
which does not care that the delimiters are paired up, use a regular expression like this:
而不是使用不关心分隔符配对的Split,使用这样的正则表达式:
<([^>]+)>
When you apply this regex to your input strings, you would capture the content of angular brackets into capturing group number 1:
将此正则表达式应用于输入字符串时,您将捕获角括号的内容到捕获组编号1:
var s = "First, Last <email1@example.com>; First, Last <email2@example.com>;";
Regex regex = new Regex(@"<([^>]+)>");
foreach (Match m in regex.Matches(s)) {
Console.WriteLine(m.Groups[1]);
}
#2
Split
won't work in this case. You need to use Regular Expressions. Try this
拆分在这种情况下不起作用。您需要使用正则表达式。试试这个
// using System.Text.RegularExpressions;
// pattern = any number of arbitrary characters between < and >.
var pattern = @"\<(.*?)\>";
var matches = Regex.Matches(addresses_Delimited, pattern);
foreach (Match m in matches) {
Console.WriteLine(m.Groups[1]);
}
#3
You can do it with split - but it's really ugly:
你可以用拆分来做 - 但它真的很难看:
var text = "First, Last <email1@example.com>; First, Last <email2@example.com>;";
var t = text.TrimEnd(';').Split(';');
foreach (var m in t)
{
Console.WriteLine(m.Split('<')[1].TrimEnd('>'));
}
Use RegularExpression instead.
请改用RegularExpression。
#4
Assuming (and this is a big assumption) that there are no ;
characters in any names or emails and that there are no ,
characters in any emails, this will work:
假设(这是一个很大的假设)没有;任何名称或电子邮件中的字符以及任何电子邮件中都没有字符,这将起作用:
using System.Linq;
using System.Net.Mail;
...
var input = "First, Last <email1@example.com>; First, Last <email2@example.com>;";
var emails = String.Join(",", input
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => new MailAddress(s).Address));
#5
Split by ";' first, then by "<" and ">".
拆分“;”首先,然后是“<”和“>”。
string inputEmails = "First1, Last1 <email1@example.com>; First2, Last2 <email2@example.com>;";
string[] inputEmailsArray = inputEmails.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string email in inputEmailsArray)
{
string[] inputEmailArray = email.Split(new char[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string emailPart in inputEmailArray)
{
string s = emailPart; // First1, Last1 // email1@example.com
}
}