1

I have string like this:

<a hidden="true" href="xxx" class=" zt-uie-session      </a>

How to extract href value in Android? It's string, not some object.

3
  • 1
    indexOf and substring Commented May 10, 2017 at 5:45
  • Is hidden and class always going to be there? Showing just one string isn't enough. Please show more examples of how the string can change, what format is the string in. Commented May 10, 2017 at 5:46
  • Please don´t use any kind of String functionallity.. If this tag isn´t really illformed as in your question just use a html parse to do the job Commented May 10, 2017 at 5:48

3 Answers 3

2

You can use this regex to find href value:

href="([^"]*)"

DEMO

Matcher m = Pattern.compile("href=\"([^\"]*)\"").matcher("<a hidden=\"true\" href=\"xxx\" class=\" zt-uie-session      </a>");
if (m.find())
    System.out.println(m.group(1));

Output

xxx
Sign up to request clarification or add additional context in comments.

Comments

0

Try below code it is working fine

        String str = "<a hidden=\"true\" href=\"xxx\" class=\" zt-uie-session></a>";
        Matcher m = Pattern.compile(" (?:href)=\"([^\"]+)").matcher(str);

        while (m.find()) {
            String result = m.group(1);
            Log.d("HREF",": "+result); //result is "xxx" 
        }

Comments

0

you can use Jsoup library for easily extract html attributes

Document doc = Jsoup.parse("<a href='link' />");
Element link = doc.select("a").first();
return link.attr("href"); //xxx

Doc here

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.