Full running example using regex:
I used pattern with 3 groups:
@"[Ff]rom:[^<]*\<([^@]+@[^>]+)>[Tt]o:[^<]*\<([^@]+@[^>]+)>[Ss]ubject: ?(.*)Thread-Topic"
)
string source = "...with mapi id 14.02.0387.000; Thu, 6 Feb 2014 09:09:33 +0100From: site <site@company.dk>To: \"nonexistingmail@doesnotexist127.dk\" <nonexistingmail@doesnotexist127.dk>Subject: can this bounceThread-Topic: can this bounceThread-Index: Ac8jEr8t3k2RouQ1RaGPCXGFcE5oNg==Date:...";
Regex pattern = new Regex("[Ff]rom:[^<]*\\<([^@]+@[^>]+)>[Tt]o:[^<]*\\<([^@]+@[^>]+)>[Ss]ubject: ?(.*)Thread-Topic");
MatchCollection mc = pattern.Matches(source);
string partFrom = ""; string partTo = ""; string subject = "";
if(mc.Count>0)
{
partFrom = mc[0].Groups[1].Value;
partTo = mc[0].Groups[2].Value;
subject = mc[0].Groups[3].Value;
}
Console.WriteLine("From: " + partFrom + " To: " + partTo + " Subject: " + subject);
I check if inside mail exist at sign (@) in my expression and extract all parts in single pattern.
If you want find only mail addresses, you can use this regex:
@"\<[^>@]+@[^>]+>"