开发者

Rewrite with regular expressions

开发者 https://www.devze.com 2023-01-23 15:19 出处:网络
Dis开发者_如何学编程position: form-data; name=\"To\" ang <ang11@google.com> -----------------------------7da3d81f160588
Dis开发者_如何学编程position: form-data; name="To"

ang <ang11@google.com>
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="CC"


-----------------------------7da3d81f160588
Content-Disposition: form-data; name="BCC"


-----------------------------7da3d81f160588
Content-Disposition: form-data; name="Subject"

MySubject
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="File"; filename=""<br/>
Content-Type: application/octet-stream


-----------------------------7da3d81f160588
Content-Disposition: form-data; name="cf_loader"

on
-----------------------------7da3d81f160588
Content-Disposition: form-data; name="File"; filename=""
Content-Type: application/octet-stream


-----------------------------7da3d81f160588
Content-Disposition: form-data; name="template_id"


-----------------------------7da3d81f160588
Content-Disposition: form-data; name="Body"

MyBody

I have written code that get information and writes in an KeyValuePair. As follow:

Key - Value

To - ang<\ang11@google.com>

CC - Empty

BCC - Empty

Subject - MySubject

and etc. My code is used of class String for this. I need to use regular expressions, but I do not understand them. Help please. Thanks.


A MIME parser should not be written with regular expressions. The problem is that you require a parser with state and that doesn't really fit regular expressions.

You can however parse parts with regular expressions, so for example:

  • First split the MIME body up into parts by splitting on the boundary: var parts = mime.Split("-----------------------------7da3d81f160588");;

  • Then, extract all the headers using a part.Split(new { "\r\n" }, 2, StringSplitOptions.None);;

  • Then, parse the headers. This may be done using a regular expression, but it's probably easier to do it like this: headerLine.Split(new [] { ':' }, 2).Select(p => p.Trim());.

Now, these separate parts can also be using regular expressions, but this will be a lot harder than a few simple Splits.

0

精彩评论

暂无评论...
验证码 换一张
取 消