不行呀筛选不到呀?这是两个正则吗? Regex Content = new Regex(@"<div[^>]*>[\s\S]*?src="(?<pic>[^"]*)"\s*alt="(?<title>[^"]*)">[\s\S]*?<span\s*class="date">(?<date>[^<]*)</span>[\s\S]*?", RegexOptions.IgnoreCase | RegexOptions.Multiline); System.Text.RegularExpressions.MatchCollection ct = Content.Matches(htmlCode); int count = 0;
foreach (Match m in ct) { mode n = new mode(); if (ct[count].Groups["src"].Value!="") { richTextBox1.AppendText = Clear.GetKeyName(m.Groups["src"].Value+ "\r\n"); } } 我代码是这样写的可是筛选不到呀
加个去贪婪,免得多匹配
Regex Content = new Regex(@"<div[^>]*>[\s\S]*?src="(?<pic>[^"]*)"\s*alt="(?<title>[^"]*)">[\s\S]*?<span\s*class="date">(?<date>[^<]*)</span>[\s\S]*?", RegexOptions.IgnoreCase | RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection ct = Content.Matches(htmlCode);
int count = 0;
foreach (Match m in ct)
{
mode n = new mode();
if (ct[count].Groups["src"].Value!="")
{
richTextBox1.AppendText = Clear.GetKeyName(m.Groups["src"].Value+ "\r\n");
}
}
我代码是这样写的可是筛选不到呀
string tempStr="";//源文本
string pattern = @"(?i)<div[^>]*>[\s\S]*?src="(?<pic>[^"]*)"\s*alt="(?<title>[^"]*)">[\s\S]*?<span\s*class="date">(?<date>[^<]*)</span>[\s\S]*?<div[^>]*>(?<all>[^<]*)<";
Match m = Regex.Match(tempStr,pattern);
string value1 = m.Groups["pic"].Value;
string value2 = m.Groups["title"].Value;
string value3 = m.Groups["date"].Value;
string value4 = m.Groups["all"].Value;
string pattern = @"(?i)<div[^>]*>[\s\S]*?src=""(?<pic>[^""]*)""\s*alt=""(?<title>[^""]*)"">[\s\S]*?<span\s*class=""date"">(?<date>[^<]*)</span>[\s\S]*?<div[^>]*>(?<all>[^<]*?)<";
string htmlCode = richTextBox2.Text;
Regex Content = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection ct = Content.Matches(htmlCode);
foreach (Match m in ct)
{
mode n = new mode();
richTextBox1.AppendText(Clear.GetKeyName(m.Groups["title"].Value + "\r\n"));
}运行结果如图
为什么第一条标题“组图:46岁周慧敏现身显气质 露黄牙显不雅”获取不到呢