我有這樣的字串:“收件人:[email protected],[email protected], [email protected], ”
我需要搜索“收件人: ”,然后決議電子郵件中的任何內容。
我知道電子郵件 regx 是\w ([- .]\w )*@\w ([-.]\w )*\.\w ([-.]\w )*,所以當我想在 regx 中添加To時,它將是
var senderRegex = new Regex(@"(?<=To: )\w ([- .]\w )*@\w ([-.]\w )*\.\w ([-.]\w )*")
但是這個只會回傳To之后的第一封電子郵件。我需要所有電子郵件的串列有什么幫助嗎?
uj5u.com熱心網友回復:
你可以用
(?:To:\s |\G(?!^))(?:,\s*)?([^\s@,] @[^\s@,] )
解釋
(?:非捕獲組To:\s匹配To:和 1 個空白字符|或者\G(?!^)斷言在上一場比賽結束時的位置以獲得連續比賽
)關閉非捕獲組(?:,\s*)?可選匹配逗號和可選的空白字符([^\s@,] @[^\s@,] )捕獲組 1,將非空白字符與單個 ??@ 字符匹配
請參閱
例如
string pattern = @"(?:To:\s |\G(?!^))(?:,\s*)?([^\s@,] @[^\s@,] )";
string input = @"To: [email protected],[email protected], [email protected] ,";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine(m.Groups[1].Value);
}
uj5u.com熱心網友回復:
如果你不使用正則運算式,你可以試試下面的代碼:
static void Main(string[] args)
{
var senders = "To: [email protected], [email protected], [email protected],";
string [] emails = senders.Split();
emails = emails.Where(i => i != "To:").ToArray();
foreach(var email in emails)
Console.WriteLine("{0}", email.Replace(",",""));
Console.ReadLine();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337106.html
