Darko Petkovski
Darko Petkovski

Reputation: 3912

Get image link and text from string

I have this string

<div><img width="100px" src="http://www.mysite.com/Content/dataImages/news/small/some-pic.png" /><br />This is some text that I need to get.</div>

and i need to get the image link and the text This is some text that I need to get.from the string above in Java. Can anybody tell me how can I do this?

Upvotes: 0

Views: 1151

Answers (4)

Darko Petkovski
Darko Petkovski

Reputation: 3912

My solution was:

String tmp=xpp.nextText();
desc=android.text.Html.fromHtml(tmp).toString();
img=FindUrls.extractUrls(tmp);

for extracting the text from the string I used:

desc=android.text.Html.fromHtml(tmp).toString();
img=FindUrls.extractUrls(tmp);

and for the link inside the string I've used this function:

 public static String extractUrls(String input) {

        String result = null;
        Pattern pattern = Pattern.compile(
            "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + 
            "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" + 
            "|mil|biz|info|mobi|name|aero|jobs|museum" + 
            "|travel|[a-z]{2}))(:[\\d]{1,5})?" + 
            "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" + 
            "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + 
            "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" + 
            "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + 
            "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" + 
            "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");

        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            result=matcher.group();
        }
        return result;
    }

Hope It will help someone that has similar problem

Upvotes: 1

NaviRamyle
NaviRamyle

Reputation: 4007

Try this, just change the patter if you must.

String str = "<div><img width=\"100px\" src=\"http://www.mysite.com/Content/dataImages/news/small/some-pic.png\" /><br />This is some text that I need to get.</div>";
Pattern p = Pattern.compile("src=\"(.*?)\" /><br />(.*?)</div>");
Matcher m = p.matcher(str);
if (m.find()) {
    String link = m.group(1);
    String text = m.group(2);
}

Upvotes: 1

rhendrix
rhendrix

Reputation: 11

If this is all you have to do there's no point in bringing in extra packages just use regex: The pattern "(?<=src=\")(.*?)(?=\")" can be used to get the link, you can modify that to give you the text.

Upvotes: 1

Sudhanshu Umalkar
Sudhanshu Umalkar

Reputation: 4202

Use regex to get what you want.

Upvotes: 1

Related Questions