filtering values from string after/before certain word in c#
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;
}
You should use a regex for this
Regex r=new Regex(@"(?<value>\d+)\s*(?<keyword>EXISTS|RECENT)");
foreach (var match in r.Matches(source).OfType(Match)) {
var parameter=int.Parse(match.Groups["value"].Value);
switch (match.Groups["keyword"].Value) {
case "EXISTS":
doExists(parameter);
break;
case "RECENT":
doRecent(parameter);
break;
}
}
You can use the following (hopefully, self-explanatory) code:
const string response =@"
* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen)
* OK [PERMANENTFLAGS ()] Flags permitted.
* OK [UIDVALIDITY 657136382] UIDs valid.
* 3 EXISTS
* 0 RECENT
* EXISTS 4 <------------------- another occurence of the word
* OK [UIDNEXT 4] Predicted next UID.
* OK [READ-ONLY] INBOX selected. (Success)";
string word = "EXISTS";
string pattern =
Regex.Escape(word) + // the word to find followed by...
@"
\s+ (?# one ore more space characters followed by... )
(?<num>\d+) (?# one or more digits. )
| (?# Or )
(?<num>\d+) (?# one or more digits, then... )
\s+ (?# one ore more space characters followed by... )"
+ Regex.Escape(word); // the word to find.
foreach (Match match in Regex.Matches(response, pattern, RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace))
{
Console.WriteLine(match.Groups["num"]);
}
class Class1
{
string str = "* 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";
public void FindString(string parm)
{
if (str.Contains(parm))
{
string[] parts = str.Split('*');
foreach (var item in parts)
{
if (item.Contains(parm))
{
string[] values = item.Split(' ');
string value = values[1];
}
}
}
}
static void Main(string[] args)
{
Class1 c = new Class1();
c.FindString("EXISTS");
}
}
精彩评论