日期:2014-05-18 浏览次数:20738 次
abdddc<div class="content" id="content5243635">还有<a href ="http://www.163.com" target="_blank">这样</a> <div class="ad">abddddsss
<div class="content" id="content\d*">((.|\n)*?)<div class="ad">
string str = @"abdddc<div class=""content"" id=""content5243635"">还有<a href =""http://www.163.com"" target=""_blank"">这样</a> 没有<a href =""http://www.163.com"" target=""_blank"">那样</a>很好 <div class=""ad"">abddddsss"; Regex reg = new Regex(@"(?is)<div[^>]*?id=""content5243635""[^>]*?>(?:\s*([^<>]*)(?:<[^>]+>([^<>]+)</[^>]+>)?)*(?=\s*<div[^>]*?class=""ad"")"); Match m = reg.Match(str); int length = m.Groups[1].Captures.Count; int minlength = m.Groups[2].Captures.Count; string result = string.Empty; for (int i = 0; i < length; i++) { result += m.Groups[1].Captures[i].Value; result += i>=minlength ? "" : m.Groups[2].Captures[i].Value; } Console.WriteLine(result); /* 还有这样没有那样很好 */