I want to get all the URLs from the given text using Patterns.WEB_URL.matcher(qrText);
What I want to do:
I am scanning a QR code,
- open the link in webView if the link contains link which contians the word “veridoc”
- showing in textView if the text scanned is not link or another link that does not contain the word “veridoc”
What I have tried:
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); } } }
In the above code,
setupWebView(String strUrl, boolean isTextAndUrlBoth)
setup webview and load url etc.openInBrowser(String url)
opens the provided URL in the browser.showQRText(String text)
shows the provided text in textView with formatting.
The Issue
When the text contains some text and more than 1 link, String url = webMatcher.group();
always fetches the first link in the text.
What I want
I want all the links from the text, and find out that which links contain the word “veridoc”. After that I would like to call the method setupWebView(url, true);
.
I am using following link and text for Example
name: Something Profession: Something link1: https://medium.com/@rkdaftary/understanding-git-for-beginners-20d4b55cc72c link 2: https://my.veridocglobal.com/login Can anyone help me to find all the links present in the text?
Advertisement
Answer
You can loop on find to find the different websites and setup arraylists with that
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); } }
Given a sample input like :
String input = "http://www.veridoc.com/1 some text http://www.veridoc.com/2 some other text http://www.othersite.com/3";
Your ArrayLists will contains :
veridocLinks : "http://www.veridoc.com/1", "http://www.veridoc.com/2" otherLinks : "http://www.othersite.com/3"