本文介绍了从字符串/某个词在C#中之前之后的过滤值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相当长的字符串,这是从IMAP请求响应,我想从中提取一些值。它通常被格式化如x someword或someword x的 - 如何获得X(它可以比一位以上)为someword(这是已知的)?响应每一个行是这样的:

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?

例:为* X字1 \ r \ñ* Y WORD2 \ r \ N,以WORD2作为参数,我想是

Example:for "* x word1\r\n* y word2\r\n", with word2 as a parameter I want to get y

全响应例如:

* 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

有关存在我想3。

推荐答案

我会用普通防爆pressions ......这里是你可以做的:

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;

这篇关于从字符串/某个词在C#中之前之后的过滤值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:18