从包含<img src=""/>标记的Java String中提取url

时间:2013-07-28 12:41:04

标签: java string extract

我希望提取以下强

http://www.01net.com/images/article/mea/150.100.790233.jpg

此字符串是以下Java字符串中第一个元素标记的url:

<img src="http://www.01net.com/images/article/mea/150.100.790233.jpg" width="150" height="100" border=0 alt="" align=left style="margin-right:10px;margin-bottom:5px;">A en croire CNet US, le gouvernement américain aurait cherché à obtenir les master keys de plusieurs acteurs du Web pour pouvoir déchiffrer les communications de leurs utilisateurs, protégées par le protocole SSL.<img width='1' height='1' src='http://rss.feedsportal.com/c/629/f/502199/s/2f34155b/mf.gif' border='0'/><div class='mf-viral'><table border='0'><tr><td valign='middle'><a href="http://share.feedsportal.com/share/twitter/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net" target="_blank"><img src="http://res3.feedsportal.com/social/twitter.png" border="0" /></a> <a href="http://share.feedsportal.com/share/facebook/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net" target="_blank"><img src="http://res3.feedsportal.com/social/facebook.png" border="0" /></a> <a href="http://share.feedsportal.com/share/linkedin/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net" target="_blank"><img src="http://res3.feedsportal.com/social/linkedin.png" border="0" /></a> <a href="http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net" target="_blank"><img src="http://res3.feedsportal.com/social/googleplus.png" border="0" /></a> <a href="http://share.feedsportal.com/share/email/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net" target="_blank"><img src="http://res3.feedsportal.com/social/email.png" border="0" /></a></td><td valign='middle'></td></tr></table></div><br/><br/><a href="http://da.feedsportal.com/r/172449334514/u/218/f/502199/c/629/s/2f34155b/kg/342/a2.htm"><img src="http://da.feedsportal.com/r/172449334514/u/218/f/502199/c/629/s/2f34155b/kg/342/a2.img" border="0"/></a><img width="1" height="1" src="http://pi.feedsportal.com/r/172449334514/u/218/f/502199/c/629/s/2f34155b/kg/342/a2t.img" border="0"/>

2 个答案:

答案 0 :(得分:11)

不要浪费模式的资源,只需使用:
工作原理:

  1. all是您要搜索的全文
  2. s是要查找的起始模式,在这种情况下它将是第一个
  3. ixall
  4. 中网址的位置
  5. 最后一行获取从all开始的ix字符串到找到的下一个"

    String all = "<img src=\"http://www.01net.com/images/article/mea/150.100.790233.jpg\""; // shortened it 
    String s = "<img src=\"";
    int ix = all.indexOf(s)+s.length();
    System.out.println(all.substring(ix, all.indexOf("\"", ix+1)));
    

答案 1 :(得分:10)

您可以使用regular expression

    String str = "<img src=\"http://www.01net.com/images/article/mea/150.100.790233.jpg\" width=\"150\" height=\"100\" border=0 alt=\"\" align=left style=\"margin-right:10px;margin-bottom:5px;\">A en croire CNet US, le gouvernement américain aurait cherché à obtenir les master keys de plusieurs acteurs du Web pour pouvoir déchiffrer les communications de leurs utilisateurs, protégées par le protocole SSL.<img width='1' height='1' src='http://rss.feedsportal.com/c/629/f/502199/s/2f34155b/mf.gif' border='0'/><div class='mf-viral'><table border='0'><tr><td valign='middle'><a href=\"http://share.feedsportal.com/share/twitter/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net\" target=\"_blank\"><img src=\"http://res3.feedsportal.com/social/twitter.png\" border=\"0\" /></a> <a href=\"http://share.feedsportal.com/share/facebook/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net\" target=\"_blank\"><img src=\"http://res3.feedsportal.com/social/facebook.png\" border=\"0\" /></a> <a href=\"http://share.feedsportal.com/share/linkedin/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net\" target=\"_blank\"><img src=\"http://res3.feedsportal.com/social/linkedin.png\" border=\"0\" /></a> <a href=\"http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net\" target=\"_blank\"><img src=\"http://res3.feedsportal.com/social/googleplus.png\" border=\"0\" /></a> <a href=\"http://share.feedsportal.com/share/email/?u=http%3A%2F%2Fwww.01net.com%2Feditorial%2F600625%2Fchiffrement-sur-le-web-fbi-et-nsa-voulaient-obtenir-les-cles-ssl-de-geants-du-net%2F%23%3Fxtor%3DRSS-16&t=Chiffrement+sur+le+Web%2C+FBI+et+NSA+voulaient+obtenir+les+cl%C3%A9s+SSL+de+g%C3%A9ants+du+Net\" target=\"_blank\"><img src=\"http://res3.feedsportal.com/social/email.png\" border=\"0\" /></a></td><td valign='middle'></td></tr></table></div><br/><br/><a href=\"http://da.feedsportal.com/r/172449334514/u/218/f/502199/c/629/s/2f34155b/kg/342/a2.htm\"><img src=\"http://da.feedsportal.com/r/172449334514/u/218/f/502199/c/629/s/2f34155b/kg/342/a2.img\" border=\"0\"/></a><img width=\"1\" height=\"1\" src=\"http://pi.feedsportal.com/r/172449334514/u/218/f/502199/c/629/s/2f34155b/kg/342/a2t.img\" border=\"0\"/>";
    Pattern p = Pattern.compile("src=\"(.*?)\"");
    Matcher m = p.matcher(str);
    if (m.find()) {
        System.out.println(m.group(1)); // prints http://www.01net.com/images/article/mea/150.100.790233.jpg
    }
相关问题