【发布时间】: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声明的样子,并在您遇到问题的地方添加一些示例文本?