简体   繁体   English

从字符串Android中提取子字符串

[英]Extract substring from string Android

I have string like this: 我有这样的字符串:

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

How to extract href value in Android? 如何在Android中提取href值? It's string, not some object. 它是字符串,而不是对象。

You can use this regex to find href value: 您可以使用此正则表达式查找href值:

href="([^"]*)"

DEMO 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

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

you can use Jsoup library for easily extract html attributes 您可以使用Jsoup库轻松提取html属性

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

Doc here Doc 在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM