C#使用Regex从电子邮件标题中提取

时间:2017-07-13 03:16:14

标签: c# regex

所以我试图从C#中的电子邮件标题中提取不同的部分。我认为我对Regex毫无希望。

我有以下内容:

[["X-Mailgun-Incoming", "Yes"], ["X-Envelope-From", "<dwdwdw@dwdwdw.com>"],["Thread-Topic", "What is status"]]

使用正则表达式我想说Thread-Topic

我在尝试:

[\"(Thread)\-(Topic)\", (\")?:[A-Z][a-z][a-z]+(\")]

理想情况下,我只想更改单词以提取其他一些参数

2 个答案:

答案 0 :(得分:0)

假设你得到原始字符串

var str = "[[\"X-Mailgun-Incoming\",\"Yes\"],[\"X-Envelope-From\",\"<dwdwdw@dwdwdw.com>\"],[\"Thread-Topic\",\"What is status\"]]";
var arr = str.Split(new string[] { "],[", "[[", "]]" }, StringSplitOptions.RemoveEmptyEntries);
var kpv = arr.Select(x => x.Split(new char[] { ',', '"' }, StringSplitOptions.RemoveEmptyEntries)).ToArray();

答案 1 :(得分:0)

如果正则表达式我会做类似的事情:

[TestMethod]
public void ParseHeadersToDictionary_MultipleHeaders_HeadersAreParsed()
{
    var emailHeaders = "[[\"X-Mailgun-Incoming\",\"Yes\"],[\"X-Envelope-From\",\"<dwdwdw@dwdwdw.com>\"],[\"Thread-Topic\",\"What is status\"]]";

    var regex = new Regex(@"\[""(?<key>.*?)"",\s?""(?<value>.*?)""]");
    Dictionary<string,string> headers = regex.Matches(emailHeaders).Cast<Match>().ToDictionary(item => item.Groups["key"].Value, item => item.Groups["value"].Value);

    var result = headers["Thread-Topic"];

    Assert.AreEqual("What is status", result);
}