正则表达式获取图像URL

时间:2013-03-31 05:30:15

标签: java regex

我想从Java字符串中获取图像的URL:

String data = "[SyndContentImpl.value=<p><img class="alignnone size-full wp-image-134291"
               title="Design Store(y): Poketo Photo" 
               src="http://3.design-milk.com/images/2013/03/storey-poketo-storefront-1.jpg" 
               alt="Design Store(y): Poketo in style fashion home 
               furnishings featured  Category" width="500" height="333" /></p>";

我试过了

String pattern = "(http://)+[\\d\\w[-./]]*(.jpg)+";

但无法从数据中获取图像的网址。 我终于需要这个网址

http://3.design-milk.com/images/2013/03/storey-poketo-storefront-1.jpg

3 个答案:

答案 0 :(得分:4)

imageLinkPattern = linkPattern | imagePostfix

所以我们有:

String pattern = "(http(s?):/)(/[^/]+)+" + "\.(?:jpg|gif|png)";

此模式只接受jpg | gif | png类型的图像

答案 1 :(得分:1)

String regex = "http(s?)://([\\w-]+\\.)+[\\w-]+(/[\\w- ./]*)+\\.(?:[gG][iI][fF]|[jJ][pP][gG]|[jJ][pP][eE][gG]|[pP][nN][gG]|[bB][mM][pP])";

Matcher m = Pattern.compile(regex).matcher(data);

if (m.find())
  System.out.println(m.group(0));

答案 2 :(得分:0)

如果要解析html,则应使用html parser


如果它不是html,你可以使用这个正则表达式

http://[^"]+?\\.(jpg|jpeg|gif|png)

[^"]匹配除"

之外的任何字符

[^"]+匹配1到多个"