7

Im trying to get HTML image tag url from the given string. There should be some regular expression to get it. But don't know how to do it. Can anyone help me on this.

e.g.

I have string like this with <br> some HTML<b>tag</b>
with <img src="http://xyz.com/par.jpg" align="left"/> image tags in it.
how can get it ?

I want only http://xyz.com/par.jpg from the string

4 Answers 4

7

Please see this question for reference. Basically it says to use:

String imgRegex = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
Sign up to request clarification or add additional context in comments.

Comments

5

I use jsoup. It is pretty easy to use and lightweight. Some versions were not Java 1.5 compatible but it appears they fixed the issue.

String html = str;
Document doc = Jsoup.parse(html);
Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png

Comments

4

Frist of All Import jsoap:

compile group: 'org.jsoup', name: 'jsoup', version: '1.7.2'

Then you can Use this:

private ArrayList pullLinks(String html) {
    ArrayList links = new ArrayList();
    Elements srcs = Jsoup.parse(html).select("[src]"); //get All tags containing "src"
    for (int i = 0; i < srcs.size(); i++) {
        links.add(srcs.get(i).attr("abs:src")); // get links of selected tags
    }
    return links;
}

Comments

1

An XMLPullParser can do this pretty easily. Although, if it is a trivially small string, it may be overkill.

     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     XmlPullParser xpp = factory.newPullParser();

     xpp.setInput( new StringReader ( "<html>I have string like this with <br> some HTML<b>tag</b> with <img src=\"http://xyz.com/par.jpg\" align=\"left\"/> image tags in it. how can get it ?</html>" ) );
     int eventType = xpp.getEventType();
     while (eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_TAG && "img".equals(xpp.getName()) {
          //found an image start tag, extract the attribute 'src' from here...
      }
      eventType = xpp.next();
     }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.