0

我在一个文本文件中有一组电子邮件。我想把身体抽出来。示例文档如下所示。

Email: 1
 ===============


  MIME-Version: 1.0
  Received: by 10.68.8.6 with HTTP; Sat, 7 Apr 2012 01:04:45 -0700 (PDT)
  Date: Sat, 7 Apr 2012 13:34:45 +0530
  Delivered-To: twistyprincess22@gmail.com
  Message-ID: <CAGibXq7_Gjqmp=jOCu2X8+Xngb5QuoqqMQ_ZKbu9jHCoJnFYgA@mail.gmail.com>
  Subject: hello
  From: twisty princess <twistyprincess22@gmail.com>
  To: twisty princess <twistyprincess22@gmail.com>
   Content-Type: multipart/alternative; boundary=047d7b33d826e6762004bd1239b5
  --047d7b33d826e6762004bd1239b5            
  Content-Type: text/plain; charset=ISO-8859-1

   hey How are you doing?

   --047d7b33d826e6762004bd1239b5       
    Content-Type: text/html; charset=ISO-8859-1

     <br><br>hey How are you doing?<br>

     --047d7b33d826e6762004bd1239b5--

所以从这篇文章中,我只想“嘿,你好吗?”。我希望使用正则表达式和 C# 来完成这项工作。谢谢

4

1 回答 1

1

使用正则表达式boundary=([^\s]+)查找边界名称

var bname = _boundaryRegex.Match(text).Groups[1].Value;

然后使用格式化文本捕获正则表达式bname

var textCapturer = new Regex(string.Format("--{0}(?<text>.*?)(?=--)",bname);
foreach(var match in textCapturer.Matches(text))
{
    Console.WriteLine(match.Groups["text"]);
}

它找到boundary参数的值,然后尝试匹配 --BOUNDARY 行之间的文本。

虽然我不建议您使用正则表达式进行这种解析。

于 2012-04-09T05:53:12.717 回答