I have quite long strings, which are response from IMAP request, and I want to extract some values from it. It's usually formatted like "x someword" or "someword x" - how get x (it can be more than one digit) for someword (which is known) ? Each "line" of response looks like:
* x someword \r\n
and my string contain a couple of this lines. What's the simplest way to extract desired values?
Example: for "* x word1\r\n* y word2\r\n", with word2 as a parameter I want to get y
Whole response example:
* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)\r\n* OK [PERMANENTFLAGS ()] Flags permitted.\r\n* OK [UIDVALIDITY xxx] UIDs valid.\r\n* 3 EXISTS\r\n* 0 RECENT\r\n* OK [UIDNEXT x] Predicted next UID.\r\n. OK [READ-ONLY] INBOX selected. (Success)\r\n
For "EXISTS" I want to get 3.
I would use Regular Expressions... here's what you can do:
string myString = "* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)\r\n* OK [PERMANENTFLAGS ()] Flags permitted.\r\n* OK [UIDVALIDITY 657136382] UIDs valid.\r\n* 3 EXISTS\r\n* 0 RECENT\r\n* OK [UIDNEXT 4] Predicted next UID.\r\n. OK [READ-ONLY] INBOX selected. (Success)\r\n";
string myWordToFind = "EXISTS";
string result = Regex.Match(myString, @"(?<MyNumber>\d+)\s*(" + Regex.Escape(myWordToFind) + ")",
RegexOptions.Compiled | RegexOptions.IgnoreCase)
.Groups["MyNumber"].Value;
So given: X markerWord\n\rmarkerWord Y
you want {X, Y}? If so, try to split by lines first, and then simply remove "markerWord".
Something roughly like:
var result = input.Split(new[]{'\n', '\r'}).Select(line => line.Replace("markerWord", string.Empty);
Updated answer: Well then I would use Regex. Simple proof of concept, I'm sure you can take it from here:
static string GetParam(string input, string param) {
var pattern = new Regex(@"[\\*](?<value>.+)" + param);
var split = input.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
var line = split.SingleOrDefault(l => pattern.IsMatch(l));
if(line != null) {
return pattern.Match(line).Groups["value"].Value.Trim();
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With