I have the below details as string i want to fetch each data separately like subject, date,... Is it possible.
Subject: Re: mr
Thread-Topic: mr
Thread-Index: AQHZNeFl9xUBUVmA0kuT2sZAbo1Dyq65WjRY
Date: Wed, 1 Feb 2023 02:15:09 +0000
Message-ID:
References:
In-Reply-To:

Naimish MakwanaRank #324 · 7.7K pointsPosted Feb 1, 2023, 12:47 AM EST
Hello Gopi,
Use below code.
var valInput = @"Subject: Re: mr Thread-Topic: mr Thread-Index: AQHZNeFl9xUBUVmA0kuT2sZAbo1Dyq65WjRY Date: Wed, 1 Feb 2023 02:15:09 +0000 Message-ID: References: In-Reply-To: "; var messageId = Regex.Match(valInput, @"Message-ID: ").Groups[1].Value;Thanks
Pasang TamangRank #449 · 5.6K pointsPosted Jan 31, 2023, 11:48 PM EST
Hi,
Looking into your content, you get multiline string. To read value of each title you have to split based on new line. And then after to separate title and value split based on :. Please refer to below code example.
static void Main(string[] args) { var msgContent = "Subject: Re: mr\r\nThread-Topic: mr\r\nThread-Index: AQHZNeFl9xUBUVmA0kuT2sZAbo1Dyq65WjRY\r\nDate: Wed, 1 Feb 2023 02:15:09 +0000\r\nMessage-ID: \r\nReferences: \r\nIn-Reply-To: "; foreach (var content in msgContent.Split("\r\n")) { var str = content.Split(':'); var heading = str[0]; var value = content.Substring(str[0].Length + 1, content.Length - (str[0].Length + 1)); Console.WriteLine(heading); Console.WriteLine(value); Console.WriteLine("---------------------------"); } Console.Read(); }In above code you see that value is not retrieve by doing str[1]. That is because your value like Subject: Re: mr will have problem. So, I use str[0] to read title and then used substring to get value.
I hope this will be helpful to you.
Regards,
Pasang
Muhammad Imran AnsariRank #326 · 7.6K pointsPosted Feb 1, 2023, 12:20 AM EST
Hi Gopi - you can extract the Message-ID from the string use the 'Regex' class and its 'Match' method to match the input string against the pattern. Regex class is available in System.Text.RegularExpressions namespace. Here is the code:
string valInput = "Your String"; string pattern = @"Message-ID: "; Match match = Regex.Match(valInput, pattern); if (match.Success) { Console.WriteLine("Message-ID: " + match.Groups[1].Value); } else { Console.WriteLine("Message-ID not found"); }GopiRank #5,293 · 304 pointsPosted Feb 1, 2023, 1:05 AM EST
It working fine if it has only one value.
If has mutliple values it pull out only the first value.
Values like below
how to fetch all value
Pasang TamangRank #449 · 5.6K pointsPosted Feb 1, 2023, 1:46 AM EST
Hi,
Either you have to generate Regex that match with your output or you can follow the way I suggested in previous response. To get message id only, you can add if condition to match the title. Below is the code example.
foreach (var content in msgContent.Split("\r\n")) { var str = content.Split(':'); var heading = str[0]; if (heading == "Message-ID") { var value = content.Substring(str[0].Length + 1, content.Length - (str[0].Length + 1)); Console.WriteLine(heading); Console.WriteLine(value); Console.WriteLine("---------------------------"); } }Regards,
Pasang