正则获取 某段 DIV 中 的内容
string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div><h1>aa</h1></div></div>";
// 获取第一个 相呼应的标记
//Regex reg = new Regex(@"<div class='left'>([\s\S]+?)<\/div>");
// 获取前后对应的标记
Regex reg = new Regex(@"(?is)<div class='left'[^>]*>(?><div[^>]*>(?<o>)|</div>(?<-o>)|(?:(?!</?div\b).)*)*(?(o)(?!))</div>");
// 获取第一条
Match first = reg.Match(html);
// 获取匹配到的所有集合
MatchCollection list = reg.Matches(html);
foreach (Match item in list)
{
string value = item.Value;
}
public static string GetElementByClassName(string htmlConetnt, string label, string className) { Regex reg = new Regex(string.Format(@"(?is)<{0} class=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}\b).)*)*(?(o)(?!))</{0}>", label, className)); Match first = reg.Match(htmlConetnt); return first.Value; } public static string GetElementById(string htmlConetnt, string label, string id) { Regex reg = new Regex(string.Format(@"(?is)<{0} id=""{1}""[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}\b).)*)*(?(o)(?!))</{0}>", label, id)); Match first = reg.Match(htmlConetnt); return first.Value; } public static string GetLabel(string htmlConetnt, string label) { Regex reg = new Regex(string.Format(@"(?is)<{0}[^>]*>(?><{0}[^>]*>(?<o>)|</{0}>(?<-o>)|(?:(?!</?{0}\b).)*)*(?(o)(?!))</{0}>", label)); Match first = reg.Match(htmlConetnt); return first.Value; }