正则表达式只找到各种特定的URL

时间:2014-09-30 22:08:26

标签: regex

RegEx将仅从'myurl.com'匹配图像src?

我越接近就是:

实时测试:http://regex101.com/r/bR0lH2/1

正则表达式:

<img.+?src=[\"'](.+?)[\"'].*?>

代码示例:

<html>
...
<br>
</b><br>
<a href="http://example.com/a/53e39f6582954.html" target="_blank"><img src="http://myurl.com/a/b/c/53e39f658291a.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f660a74d.html" target="_blank"><img src="http://myurl.com/a/b/c/53e39f660a716.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f66a1cdd.html" target="_blank"><img src="http://others.net/a/b/c/53e39f66a1ca6.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f67265ee.html" target="_blank"><img src="http://others.com/a/b/c/53e39f67265b3.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f67ad28f.html" target="_blank"><img src="http://myurl.com/a/b/c/53e39f67ad255.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f6838b67.html" target="_blank"><img src="http://myurl.com/a/b/c/53e39f6838b2b.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f68c564f.html" target="_blank"><img src="http://others.com/a/b/c/53e39f68c5618.jpg" alt="" border="0"></a>
 <a href="http://example.com/a/53e39f69630bc.html" target="_blank"><img src="http://myurl.com/a/b/c/53e39f6963085.jpg" alt="" border="0"> ... ... 
...
</html>

1 个答案:

答案 0 :(得分:0)

你几乎做对了:

<img[^>]+?src\s*=\s*[\"'](http:\/\/myurl\.com.+?)[\"'].*?>

regex101

这个会更好一点:

<img[^>]+?src\s*=\s*([\"'])(http:\/\/myurl\.com.+?)\1.*?>

regex101

确保引号匹配,但它与$2中的网址匹配。