Date: Sun, 31 Oct 2010 21:12:24 +0600
From: [email protected]
X-Priority: 3 (Normal)
Message-ID: <[email protected]>
To: [email protected]
Subject: MySubject
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1251
Content-Transfer-Encoding: quoted-printableHello from Jim.
帮助获取信息。我需要在数组中包含以下内容:
Sun, 31 Oct 2010 21:12:24
[email protected]
[email protected]
MySubject
Hello from Jim
如果可以通过正则表达式实现。谢谢。
更新:不一定通过正则表达式。
最佳答案
您可以使用以下代码创建键与值的字典。它假设键是唯一的。
var lookupDictionary=
Regex
.Matches(src, "(.+?): (.+)", RegexOptions.Multiline)
.Cast<Match>()
.ToDictionary(m => m.Groups[1].Value,m => m.Groups[2].Value);
因此,要获取 To
字段,您可以:
lookupDictionary["To"]
要抓取正文,您需要一个单独的表达式:
var match=Regex.Match(src, "(?<=.*\n\n).*");
var body=match.Success ? match.Value : null;
您可能会处理 CRLF 行结尾,在这种情况下,正则表达式将是:
var match=Regex.Match(src, "(?<=.*\r\n\r\n).*");
var body=match.Success ? match.Value : null;
关于c# - 正则表达式的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4077052/