Search code examples
javaimagehref

Copying a set of strings to local variables based on a keyword


I need to extract http links from a set of text information. Particular phrases which has image extenstions(.jpg, .png)

For example, I want alone "http://www.mywedding.com/blog/wp-content/gallery/p_867/bridal-party-white-flowers-40306.jpg"

from the below set of thing

 <p><a href="http://www.mywedding.com/blog/wp-content/gallery/p_867/bridal-party-white-flowers-40306.jpg" target="_blank"><a href="http://www.mywedding.com/blog/wp-content/gallery/p_867/bridal-party-white-flowers-40306.jpg">http://www.mywedding.com/blog/wp-content/gallery/p_867/bridal-party-white-flowers-40306.jpg</a></a> (from <a href="http://cameroningalls.com/emily-meeko-tiburon/" target="_blank">this wedding by Cameron Ingalls</a>)</p>

If those are img tags, i can get it from simple appraoch

String s="<img src="https://i.sstatic.net/SUSRH.jpg"/> 

s=s.replace("<img src=", "");
s=s.replace("/>", "");

But I am confused to get through first scenario, Any suggestions for it?


Solution

  • Do you already have the raw information in a single string? This looks like a classic job for regular expressions. I would check this site,

    9: HTML tag Regular Expression Pattern http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/

    And there's a pretty good explanation of how to use it here: http://www.mkyong.com/regular-expressions/how-to-extract-html-links-with-regular-expression/