<div class="content" title="2013-03-26 12:00:42">我需要的内容</div>正html 是很长的。有很多组div内容我需要全部取出。 Regex regImg = new Regex(@"<div class=""content"" title="".*"">?<imgUrl>(.*)</div>", RegexOptions.IgnoreCase);
// 搜索匹配的字符串sHtmlText 为html内容
MatchCollection matches = regImg.Matches(sHtmlText); int i = 0;
string[] sUrlList = new string[matches.Count]; // 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value; return sUrlList;我这样写错了。求指导。
html正则提取内容
// 搜索匹配的字符串sHtmlText 为html内容
MatchCollection matches = regImg.Matches(sHtmlText); int i = 0;
string[] sUrlList = new string[matches.Count]; // 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value; return sUrlList;我这样写错了。求指导。
html正则提取内容
解决方案 »
- 关于frameset传值问题
- 请教一个简单的点击按钮,让标签隐藏
- winform程序遇到奇怪的问题,请高手指教
- 15分钟结贴哦..只要解答对我有用的,都有分。刚接触javascript,高手帮下我。。谢谢
- 关于word控制的问题!Merge的用法
- .net资源释放以及COM控件资源释放的原理
- 请教下正则表达式图片替换问题..
- this.dgvListtable.Rows[e.RowIndex].HeaderCell.Value显示行号这个应该写在什么事件中
- 关于Thread中的方法Interrupt Sleep应用问题
- 在线等待,按钮问题?
- C#如何快速的读写mdb数据库?
- 怎么判断Datagridview中的数据是否已保存到数据库表中
private Match GetMatch(string input, string pattern, string find)
{
string _pattn = Regex.Escape(pattern);
_pattn = _pattn.Replace(@"\[变量]", @"[\s\S]*?");
_pattn = Regex.Replace(_pattn, @"((\\r\\n)|(\\ ))+", @"\s*", RegexOptions.Compiled);
if (Regex.Match(pattern.TrimEnd(), Regex.Escape(find) + "$", RegexOptions.Compiled).Success)
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+)");
else
_pattn = _pattn.Replace(@"\" + find, @"(?<TARGET>[\s\S]+?)");
Regex r = new Regex(_pattn, RegexOptions.IgnoreCase | RegexOptions.Compiled);
Match m = r.Match(input);
return m;
}
以上是一个采集内容的规则之一
首先我们要定义内容的规则
<div class="content" title="[变量]">[内容]</div>GetMatch(要采集的内容,规则,"[内容]").Groups["TARGET"].Value;
input 是html的内容吗?
规则 就是 整个的正则表达式吗?
[内容] 是什么内容?我的意向是 去整个html 里面 所有 ‘我需要的内容’ 的值
//经过不懈的努力,终于完成 谢谢各位的帮忙。
public static string[] GetHtmlImageUrlList(string sHtmlText)
{
string pattern = @"<div class=[^<>]+>(?<imgUrl>[^<>]+)</div>";
Regex regImg = new Regex(pattern, RegexOptions.IgnoreCase);
// 搜索匹配的字符串
MatchCollection matches = regImg.Matches(sHtmlText); int i = 0;
string[] sUrlList = new string[matches.Count]; // 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value;
return sUrlList;
}