如何将以下字符串拆分为键和值对?

时间:2014-04-24 05:53:50

标签: c# .net string

我有一个跟随字符串,因为我需要转换键值对中的特定值

--Wj0WGWaKTUC0ynTbTxQl8Azz //this value is changing every request
Content-Disposition: form-data; name="key"

donny/uploads/11.txt
--Wj0WGWaKTUC0ynTbTxQl8Azz
Content-Disposition: form-data; name="acl"

private
--Wj0WGWaKTUC0ynTbTxQl8Azz
Content-Disposition: form-data; name="Content-Type"

text/plain
--Wj0WGWaKTUC0ynTbTxQl8Azz
Content-Disposition: form-data; name="AWSAccessKeyId"

12345
--Wj0WGWaKTUC0ynTbTxQl8Azz
Content-Disposition: form-data; name="Policy"

eyJleHBpcmF0aW9uIjogIjIwMTUtMDQtMDFUMDA6MDA6MDBaIiwNCiAgImNvbmRpdGlvbnMiOiBbIA0KICAgIHsiYnVja2V0IjogIm9uZWF3c3Rlc3QifSwgDQogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgImRvbm55L3VwbG9hZHMvIl0sDQogICAgeyJhY2wiOiAicHJpdmF0ZSJ9LA0KICAgIFsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0L3BsYWluIl0sICAgDQogICAgWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzZdDQogIF0NCn0=
--Wj0WGWaKTUC0ynTbTxQl8Azz
Content-Disposition: form-data; name="Signature"

FCCjX7n+MKc4XmRvAt05f3Zk9Vk=
--Wj0WGWaKTUC0ynTbTxQl8Azz 

在上面我需要两个值,一个是Name =“Key”值,另一个是在下一行值之后

像fallwoing formate

"key" donny/uploads/11.txt

"acl" private

"Content-Type" text/plain

"AWSAccessKeyId" 12345

"Policy" eyJleHBpcmF0aW9uIjogIjIwMTUtMDQtMDFUMDA6MDA6MDBaIiwNCiAgImNvbmRpdGlvbnMiOiBbIA0KICAgIHsiYnVja2V0IjogIm9uZWF3c3Rlc3QifSwgDQogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgImRvbm55L3VwbG9hZHMvIl0sDQogICAgeyJhY2wiOiAicHJpdmF0ZSJ9LA0KICAgIFsiZXEiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0L3BsYWluIl0sICAgDQogICAgWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzZdDQogIF0NCn0=

"Signature" FCCjX7n+MKc4XmRvAt05f3Zk9Vk=

提前致谢

普尔纳

1 个答案:

答案 0 :(得分:0)

static KeyValuePair<string, string>[] ExtractInfo(string text)
{
    return Regex.Matches(text, @"Content-Disposition: form-data; name=""(?<name>.*)""(\r?\n){2}(?<value>.*)")
                .Cast<Match>()
                .Select(m => new KeyValuePair<string, string>(m.Groups["name"].Value, m.Groups["value"].Value))
                .ToArray();
}

解释和演示:

Content-Disposition: form-data; name="(?<name>.*)"(?:\r?\n){2}(?<value>.*)

Regular expression visualization

Debuggex Demo