日期:2014-05-19  浏览次数:20769 次

请问这个正则表达式该如何写??
我想要从html文档中截取,一个图片的地址.比如
<html>
...
<img   src= "commonFile/images/img.aspx?id=23456 "> </img>
...
</html>

请问如何获得commonFile/images/img.aspx?id=23456
????

------解决方案--------------------
try

      string yourStr = ............;
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;
}
------解决方案--------------------
因为你前面的都一样commonFile/images/img.aspx?id= 

你只取ID就行了 如下是取ID的

string yourStr = "................... ";
MatchCollection mc = Regex.Matches(yourStr, @ "img.aspx\?id=(? <yID> \d+) " " ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[ "src "].Value + "\n ";
}