我正在寻找一个正则表达式来从电子邮件地址列表中生成字符串列表。
var emailAddress = "joe@sad.com; joe@happy.com; joe@elated.com";
想要创建一个字符串列表
List<string> emails = new List<string>();
包含
email[0] = joe@sad.com
email[1] = joe@happy.com
email[2] = joe@elated.com
答案 0 :(得分:2)
你不需要正则表达式!
var emailAddress = "joe@sad.com; joe@happy.com; joe@elated.com";
var emails = emailAddress.Split(';').Select(i => i.Trim()).ToList();
或者,如果您不想修剪:
var emails = emailAddress.Split(new[]{' ',';'}, StringSplitOptions.RemoveEmptyEntries)
.ToList();
答案 1 :(得分:1)
试试这个:
string emailAddress = "joe@sad.com; joe@happy.com; joe@elated.com";
string[] emails = emailAddress.Replace(" ", "").Split(';'); // this will put every email in one position
foreach(string email in emails){
// your code for every email
}
你的字符串电子邮件将是一个字符串数组,其位置你也可以为cicle做正常的i = 0; I&LT; ......等等。
希望有所帮助:)
答案 2 :(得分:-1)
考虑以下代码......
var emailAddress = "joe@sad.com; joe@happy.com; joe@elated.com";
List<string> emails = new List<string>();
emails = Regex.Matches(emailAddress, @"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})")
.Cast<Match>()
.Select(m => m.Groups[0].Value)
.ToList();
祝你好运!