日期:2014-05-17  浏览次数:20688 次

求一字符串解析方法问题
str="#0||1.txt||0.00 KB||~/Upload/FileUpload20121007073636062.txt||file-0#0||2.txt||0.00 KB||~/Upload/FileUpload20121007073640375.txt||file-1"

我想对上面的字符串先通过#0进行解析出两条数据,再通过||进行解析将得到的数据放入一个list集合中,最好是用正则表达式来实现,这样可以去掉中间的空格之类的,谢谢!

我先是这样写的
string pattern = "(?is)(?<=" + Regex.Escape("#0") + ").+?(?=" + Regex.Escape("#0") + ")";
  Regex reg = new Regex(pattern);
  MatchCollection mc = reg.Matches(str);
  foreach (Match m in mc)
  {
  这中间的||正则怎么写,同时要去掉空格
  }

------解决方案--------------------
这个正则真不会,先帮顶!
------解决方案--------------------
C# code
string str = "#0||1.txt||0.00 KB||~/Upload/FileUpload20121007073636062.txt||file-0#0||2.txt||0.00 KB||~/Upload/FileUpload20121007073640375.txt||file-1";
var query = str.Split('#', StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries)).Select(x => new { filename = x[1], size = x[2], path = x[3], fileid = x[4] });
foreach (var item in query)
{
    Response.WriteLine(item.filename);
    Response.WriteLine(item.size);
    ...
}