Regex timeRegex = new Regex("?<=<div[^>]+class=['"][^'">]*l_02['"][^>]*>)[^<]*(?=</div>");
<script type="text/javascript"> var test = "<div class=\"l_def l_02\">北京演唱会</div>"; var reg = /<div(?!class=\"l_def l_02\"[^>]*>)[^>]+>(((?!<\/div>).)+)/ig; var mts; while (mts = reg.exec(test)) { document.write(mts[1]); } </script>
static void Main(string[] args) { string html = @"<div class=""l_def l_02"">北京演唱会</div>"; MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<div(?!class=""l_def l_02""[^>]*>)[^>]+>)(?:(?!</div>).)+"); foreach (Match m in mc) { Console.WriteLine(m.Value); } Console.ReadKey(); }
static void Main(string[] args) { string html = @"<div class=""l_def l_02"">北京演唱会</div>"; MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<div(?!class=""(?![^""]*l_02[^""]*"")l_02""[^>]*>)[^>]+>)((?!</div>).)+"); foreach (Match m in mc) { Console.WriteLine(m.Value); } Console.ReadKey(); }
之前没看到只判断l_02。走弯路了。还是走回来吧 static void Main(string[] args) { string html = @"<div class=""l_def l_02"">北京演唱会</div>"; MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<div[^>]*?class=""[^""]*?l_02[^""]*""[^>]*>)((?:(?!</div>).)+)"); foreach (Match m in mc) { Console.WriteLine(m.Value); } Console.ReadKey(); }
第16楼的代码貌似把所有<div>中的内容都取出来了额
try... Regex reg = new Regex(@"(?is)<div[^>]*?class=(['""])[^'"">]*?l_02[^'"">]*\1[^>]*>.*?</div>"); MatchCollection mc = reg.Matches(yourStr); foreach (Match m in mc) { richTextBox2.Text += m.Value + "\n"; }
取出<div>中的内容但是class里要有l_02
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script type="text/javascript">
function Show() {
var items = document.getElementsByTagName("div");
var result = "";
for (var i = 0; i < items.length; i++) {
if (items[i].className.indexOf("l_02") >= 0) {
result += (items[i].className + ":" + items[i].innerHTML);
}
}
alert(result);
}
</script>
</head>
<body>
<div class="xcdcs_l_02">水电费快捷方式的</div>
<div class="xx_l_02">的简介水电费</div>
<div class="sadsda">sasdads</div>
<button onclick="Show()">点击</button>
</body>
</html>
(?<=<div[^>]+class=['"][^'">]*l_02['"][^>]*>)[^<]*(?=</div>)
var test = "<div class=\"l_def l_02\">北京演唱会</div>";
var reg = /<div(?!class=\"l_def l_02\"[^>]*>)[^>]+>(((?!<\/div>).)+)/ig;
var mts;
while (mts = reg.exec(test))
{
document.write(mts[1]);
}
</script>
static void Main(string[] args)
{
string html = @"<div class=""l_def l_02"">北京演唱会</div>";
MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<div(?!class=""l_def l_02""[^>]*>)[^>]+>)(?:(?!</div>).)+");
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
{
string html = @"<div class=""l_def l_02"">北京演唱会</div>";
MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<div(?!class=""(?![^""]*l_02[^""]*"")l_02""[^>]*>)[^>]+>)((?!</div>).)+");
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
static void Main(string[] args)
{
string html = @"<div class=""l_def l_02"">北京演唱会</div>";
MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<div[^>]*?class=""[^""]*?l_02[^""]*""[^>]*>)((?:(?!</div>).)+)");
foreach (Match m in mc)
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}