有下例Html代码
<div>
<img alt='' src="/UpLoad/image/111.jpg" /><img alt='' src='/UpLoad/image/222.jpg' /><img
src='./UpLoad/image/333.jpg' alt='' /></div>
<div>
<img alt='' src='/UpLoad/image/444.jpg' />
<img src='../sa/UpLoad/image/333.jpg' alt='' />
</div>
现在要取出img中的src。正则的写法如下:
<img[^>]*src\s*=\s*['"]([\w/\-\.]*)['"][^>]*
解释:
<img[^>]* :查找以<img开头,以>终止的内容。
src\s*=\s*['"]([\w/\-\.]*)['"][^>]*:查找src开头,以>终止的内容,并取src中内容。
\s* :0或多个空格
['"] :取一个'或“。
([\w/\-\.]*) :取单词字符,包含_/-.等字符。(),表示用Groups方式取到值。
用法:
private void FindImageInXmlMethod()
{
string ImgXml = " <div><img alt='' src=\"/UpLoad/image/111.jpg\" /><img alt='' src='/UpLoad/image/222.jpg' /><img src='./UpLoad/image/333.jpg' alt='' /></div>"
+ @"<div><img alt='' src='/UpLoad/image/444.jpg' />
<img src='../sa/UpLoad/image/333.jpg' alt='' />
</div>";
string RegEx = "<img[^>]*src\\s*=\\s*['\"]([\\w/\\-\\.]*)['\"][^>]*";
Regex imgRegex = new Regex(RegEx,RegexOptions.IgnoreCase);
MatchCollection matchCollection = imgRegex.Matches(ImgXml);
foreach (Match matchItem in matchCollection)
{
string val = matchItem.Value;
val = matchItem.Groups[1].Value;
Response.Write(val + "<br/>");
}
}
结果:
/UpLoad/image/111.jpg
/UpLoad/image/222.jpg
./UpLoad/image/333.jpg
/UpLoad/image/444.jpg
../sa/UpLoad/image/333.jpg