日期:2014-05-18 浏览次数:21015 次
<tr> <td>
<img border='0' src='/_layouts/images/reportsup.gif' alt='Reports to' />
</td> <td>User1 </td>
</tr>
<tr> <td>
<img src="/_layouts/images/reportsdown.gif" alt="Manager">
</td> <td>User2 </td>
</tr>
<tr> <td>
<table> <tr> <td>
<span/>
</td> </tr> </table>
</td> <td>User3 </td>
</tr>
//取出图片路径 MatchCollection mc; Regex r = new Regex(@"<img.*?src=(?:""|')?(.*?\.(?:jpg|gif)).*?"); mc = r.Matches("这里填写你要找的HTML代码 赋值给一个字符串"); for(int i = 0;i<mc.count;i++) { //mc[i]就是图片路径了 }
------解决方案--------------------
Regex xxx=new Regex("src=('|\")(?<text>.*)('|\")( alt)"); string[,] arr = new string[,] { { "","" }, { "","" } }; int i = 0; int j = 0; foreach (Match m in xxx.Matches(str)) //str为HTML文档 { //MessageBox.Show(xxx.Match(str).Groups["text"].Value); arr[i, j] = m.Groups["text"].Value; arr[i++,j + 1] = "User" + i.ToString(); } string x=""; //以下为检测用 for(int ix=0;ix<i;ix++) { x+=arr[ix,0].ToString(); x+="\r\n"; x+=arr[ix,1].ToString(); x+="\r\n"; } MessageBox.Show(x);
------解决方案--------------------
/// <summary>
/// 返回一组标签中指定属性的值小写格式
/// 属性值必须被双引号括起来。
/// </summary>
/// <param name="Tag">一组标签 </param>
/// <param name="TagName">属性名称 </param>
/// <returns>属性的小写值 </returns>
private string TagVal(string tag, string tagName)
{
Regex rt = new Regex(@"(? <Keyword>\w+)\s*=\s*(? <Value>" + "\"" + "[^\"]*\")", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match mt in rt.Matches(tag)) {
if (mt.Groups["Keyword"].ToString().Trim().ToLower() == tagName.ToLower()) {
return mt.Groups["Value"].ToString().ToLower().Replace("\"", "");
}
}
return null;
}
///去掉所以的html标签,如 <a>aabcd </a> 返回的是aabcd
private string DropHTML(string strHtml)
{
return Regex.Replace(strHtml, " <[^>]*>", "");
}