Search code examples
javastringextract

Extract url from Java String containing <img src> tag


I would like to extract the folowing String :

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

This string is the url of the first element tag in the following Java string :

<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"/>

Solution

  • Here is a quick and dirty solution using the String API instead of regular expressions which is faster.

    Working principle:

    1. all is the whole text you want to search for

    2. s is the start pattern to look for, in this case it will be the first <img ... tag. If you have multiple img, consider iteration or extending the string to possible id="" or class="" tags

    3. ix is the position of the URL in all

    4. the last line gets the String from all starting at ix to the next " it finds

      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)));
      

    EDIT: A bit more details for advanced readers. As stated in other answers and comments you should not use the String API to parse HTML, as there are many specifics that are hard to catch. Note that regex won't help you either as it is a type-3 Chomsky language (regular) and therefore a subset of HTML which is type-2 (context sensitive, see Wiki). In production use a DOM-parser like jsoup. For quick hacking or known style a String API solution will probably work just fine and add less overhead.