<p>今天天气不错哦</p><p><img style="float:none" title="大众健身.jpg" border="0" hspace="0" src="/xjk/Web/ueditor/ueditornet/net/upload/2012-06-18-15f3c36695-4802-45d9-bbf6-081684b67add.jpg" /></p>
<p><img border="0" hspace="0" src="http://img.baidu.com/hi/jx2/j_0015.gif" /></p>上面是一段html代码,本身长度不定,现在我想从里面截取得到第一张图片,也就是:
<img style="float:none" title="大众健身.jpg" border="0" hspace="0" src="/xjk/Web/ueditor/ueditornet/net/upload/2012-06-18-15f3c36695-4802-45d9-bbf6-081684b67add.jpg" />,应该怎么做呀???烦请各位路人,高手帮忙看看,指教下小弟,先跪谢隆恩!!!在线等,急啊。
<p><img border="0" hspace="0" src="http://img.baidu.com/hi/jx2/j_0015.gif" /></p>上面是一段html代码,本身长度不定,现在我想从里面截取得到第一张图片,也就是:
<img style="float:none" title="大众健身.jpg" border="0" hspace="0" src="/xjk/Web/ueditor/ueditornet/net/upload/2012-06-18-15f3c36695-4802-45d9-bbf6-081684b67add.jpg" />,应该怎么做呀???烦请各位路人,高手帮忙看看,指教下小弟,先跪谢隆恩!!!在线等,急啊。
这样取第一条数据就行了
代码如下:
test.InnerText = "<p>今天天气不错哦</p><p><img style=\"float:none\" title=\"大众健身.jpg\" "+
"border=\"0\" hspace=\"0\" src=\"/xjk/Web/ueditor/ueditornet/net/upload/2012-06-18-15f3c36695-4802-45d9-bbf6-081684b67add.jpg\" />"+
"</p><p><img border=\"0\" hspace=\"0\" src=\"http://img.baidu.com/hi/jx2/j_0015.gif\" /></p>";
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(?is)<img\b[^>]*/>"); string a = regex.Replace(test.InnerText, "");
Response.Write(a);
得到结果:
今天天气不错哦
图片一
图片二
string text = "<p>今天天气不错哦</p><p><img style=\"float:none\" title=\"大众健身.jpg\" " +
"border=\"0\" hspace=\"0\" src=\"/xjk/Web/ueditor/ueditornet/net/upload/2012-06-18-15f3c36695-4802-45d9-bbf6-081684b67add.jpg\" />" +
"</p><p><img border=\"0\" hspace=\"0\" src=\"http://img.baidu.com/hi/jx2/j_0015.gif\" /></p>"; Regex regex = new Regex(@"(?is)<img\b[^>]*/>");
MatchCollection mc = regex.Matches(text);
List<string> list = new List<string>();
foreach (Match m in mc)
{
list.Add(m.Value);
}
Response.Write(list.First());