日期:2014-05-17 浏览次数:20388 次
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("utf-8"));//读取txt string pattern_1 = @"(?i)<div[^>]*?class=(['""]?)game_info1\1[^>]*?>\s*?<p>[\s\S]*?<a[^>]*?>([^<>]+?)</a>[^::]*?[::](\d+)[\s\S]*?</p>"; pattern_1 += @"\s*?<p>[^<>]+?(<a[^>]*?>(?<text>[^<>]+?)</a>\s*?)*?</p></div>"; //tempStr = Regex.Replace(tempStr,pattern,"<tr>$0</tr>"); foreach (Match m in Regex.Matches(tempStr, pattern_1)) { //循环输出 // string t1 = m.Groups[2].Value;//儿童小游戏 string t2 = m.Groups[3].Value;//205 string t3 = string.Join(",", m.Groups["text"].Captures.Cast<Capture>().Select(a => a.Value));//幼儿教育,学习类 }
------解决方案--------------------
第二个注意你div的开始和结束地方
string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("utf-8"));//读取txt string pattern_2 = @"(?isx)<div[^>]*class=(['""]?)operate\1[^>]*?>((?><div[^>]*> (?<Open>)|</div> (?<-Open>)|(?:(?!</?div\b).)*)*(?(Open)(?!)))</div>"; string result = Regex.Replace(Regex.Match(tempStr, pattern_2).Groups[2].Value, @"<!--[\s\S]*?-->",string.Empty); /* * <div id="GameKey"> <ul><li><SPAN class=ico_c_mouseleft></SPAN>鼠标操作</li></ul><div class='clear'></div><div class="game_cz_fg"></div> <div class="clear"></div> </div> 鼠标控制移动,左键点击调下踩跷跷板,注意要踩到正确的答案哦! */