【问题标题】:How to extract all the URLs from the text in android如何从android中的文本中提取所有URL
【发布时间】:2019-05-08 07:33:43
【问题描述】:

我想使用Patterns.WEB_URL.matcher(qrText);从给定文本中获取所有 URL

我想做的事:

我正在扫描二维码,

  • 如果链接包含包含单词“veridoc”的链接,请在 webView 中打开链接
  • 如果扫描的文本不是链接或其他不包含单词“veridoc”的链接,则在 textView 中显示

我尝试过的:

private void initialize() {
    if (getIntent().getStringExtra(Constants.KEY_LINK) != null) {
        qrText = getIntent().getStringExtra(Constants.KEY_LINK);
        webMatcher = Patterns.WEB_URL.matcher(qrText);
    }

    if (qrText.contains("veridoc") && webMatcher.matches()) {
            //if qr text is veridoc link
            Log.e("veridoc link", qrText);
            setupWebView(qrText, false);
        } else if (webMatcher.matches()) {
            //if qr text is link other than veridoc
            Log.e("link", qrText);
            openInBrowser(qrText);
            finish();
        } else if  (qrText.contains("veridoc") && webMatcher.find()) {
            //if qrText contains veridoc link + other text.
            String url = webMatcher.group();

            if (url.contains("veridoc")) {
                Log.e("veridoc link found", url);
                setupWebView(url, true);
            } else
                showQRText(qrText);
        } else {
            //the qrText neither is a link nor contains any link that contains word veridoc
            showQRText(qrText);
        }
    } 
}

在上面的代码中,

  • setupWebView(String strUrl, boolean isTextAndUrlBoth)设置webview和加载url等

  • openInBrowser(String url) 在浏览器中打开提供的 URL。

  • showQRText(String text) 在带有格式的 textView 中显示提供的文本。

问题

当文本包含一些文本和多个链接时,String url = webMatcher.group(); 总是获取文本中的第一个链接。

我想要什么

我想要文本中的所有链接,并找出哪些链接包含单词“veridoc”。之后我想调用方法setupWebView(url, true);

我正在使用以下链接和文本作为示例

名称:某物 职业:东西 链接1:https://medium.com/@rkdaftary/understanding-git-for-beginners-20d4b55cc72c 链接2:https://my.veridocglobal.com/login 谁能帮我找到文本中存在的所有链接?

【问题讨论】:

  • 使用您正在使用的链接添加示例文本
  • 代码中的问题是,您只在if 循环中调用webMatcher.find(),因为它只返回第一个匹配的URL,其中可能有也可能没有veridoc .因此,您将需要在 while 循环中使用 webMatcher.find() 来提取所有匹配项并检查匹配的 URL 是否包含 veridoc 您能否展示您的 Patterns.WEB_URL 声明的样子,并在您遇到问题的地方添加一些示例文本?

标签: java android regex


【解决方案1】:

您可以循环查找以查找不同的网站并使用它设置数组列表

Matcher webMatcher = Patterns.WEB_URL.matcher(input);
ArrayList<String> veridocLinks = new ArrayList<>();
ArrayList<String> otherLinks = new ArrayList<>();

while (webMatcher.find()){
    String res = webMatcher.group();
    if(res!= null) {
        if(res.contains("veridoc")) veridocLinks.add(res);
        else otherLinks.add(res);
    }
}

给定一个示例输入,例如:

String input = "http://www.veridoc.com/1 some text http://www.veridoc.com/2 some other text http://www.othersite.com/3";

您的 ArrayLists 将包含:

veridocLinks : "http://www.veridoc.com/1", "http://www.veridoc.com/2"
otherLinks : "http://www.othersite.com/3"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2011-11-27
    相关资源
    最近更新 更多