--ze6hbLXgtpQbZnvTC98zPbfLo3Zr_357l55nxx
Content-Disposition: form-data; name="expressDealId"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
111209511524_1
--ze6hbLXgtpQbZnvTC98zPbfLo3Zr_357l55nxx
Content-Disposition: form-data; name="expressCompanyId"
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
wu023shangcheng
--ze6hbLXgtpQbZnvTC98zPbfLo3Zr_357l55nxx--
例如上述字符串,怎样能获取到
name="expressDealId" 111209511524_1
name="expressCompanyId" wu023shangcheng
public void test() { string s = ""; int start = 0; List<data> list = new List<data>(); do { int index = s.IndexOf("name=\"", start); start = index + 6; if (start <= 6 || start > s.Length) break; int end = s.IndexOf("\"", start); if (end < 0) break; data item = new data(); item.Name = s.Substring(start, end - start); start = end; index = s.IndexOf("\r\n\r\n", start); start = index + 4; end = s.IndexOf("\r\n", start); if (start <= 4 || end < 0) break; item.Value = s.Substring(start, end - start); list.Add(item); } while (start > 6); } public class data { public string Name { set; get; } public string Value { set; get; } }
没看明白啥意思?
就是说怎么样解析,可以得到这个结果name="expressDealId" 111209511524_1
name="expressCompanyId" wu023shangcheng
先用换行符把字符串分解成字符串数组,FOR循环,然后如果有name关键字就截取name的值(最好是正则表达式),然后I+3(或者判断Content-Transfer-Encoding关键字的下一个循环)取值
用正则表达式实现的C#代码如下:
[Test] public void RegexTest() { var text = System.IO.File.ReadAllText("f:\\temp\\text.txt"); var matches = Regex.Matches(text, @"(name=""[^""]+"")(?:\r\n[^\r\n]+){2}\r\n([^\r\n]+)"); foreach (Match match in matches) { Console.WriteLine(match.Groups[1] + " " + match.Groups[2]); } }