我想要从html文档中截取,一个图片的地址.比如
<html>
...
<img src="commonFile/images/img.aspx?id=23456"></img>
...
</html>请问如何获得commonFile/images/img.aspx?id=23456
????
<html>
...
<img src="commonFile/images/img.aspx?id=23456"></img>
...
</html>请问如何获得commonFile/images/img.aspx?id=23456
????
string yourStr = ............;
string resultStr = "";
Match m = Regex.Match(yourStr, @"<img\s+src=([""']?)(?<src>[^""'\s]*)\1?[^>]*>", RegexOptions.IgnoreCase);
if (m.Success)
{
resultStr = m.Groups["src"].Value;
}
你的html文档中就只有这一个<img...>吗,如果是,用上面的即可,如果不是,那么说明一下,要根据什么来找到你想要的这个图片地址
string resultStr = "";
Match m = Regex.Match(yourStr, @"<img\s+src=([""']?)(?<src>commonFile/images/img\.aspx\?id=\d+)\1?[^>]*>", RegexOptions.IgnoreCase);
if (m.Success)
{
resultStr = m.Groups["src"].Value;
}
MatchCollection mc = Regex.Matches(yourStr, @"img.aspx\?id=(?<yID>\d+)""", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["src"].Value + "\n";
}
MatchCollection mc = Regex.Matches(yourStr, @"img.aspx\?id=(?<yID>\d+)""", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["yID"].Value + "\n";
}
? 谢谢过客,你的正则很强啊----------
他的正则 是只取一个 你不是有很多img吗 我写才是取 多个ID号
你再用commonFile/images/img.aspx?id= 组合一下就行咯
MatchCollection mc = Regex.Matches(yourStr, @"<img\s+src=([""']?)(?<src>commonFile/images/img\.aspx\?id=\d+)\1?[^>]*>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["src"].Value + "\n";
}