Reputation: 2884
I want to check if the text has more than one link or not so for that i started with the following code:
private static void twoOrMorelinks(String commentstr){
String urlPattern = "^.*((?:http|https):\\/\\/\\S+){1,}.*((?:http|https):\\/\\/\\S+){1,}.*$";
Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(commentstr);
if (m.find()) {
System.out.println("yes");
}
}
But the above code is not very professional and I am looking for something as follow:
private static void twoOrMorelinks(String commentstr){
String urlPattern = "^.*((?:http|https):\\/\\/\\S+){2,}.*$";
Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(commentstr);
if (m.find()) {
System.out.println("yes");
}
}
But this code does not work for instance I expect the code to show match for the following text but it does not:
They say 2's company watch live on...? http://www.le testin this code http://www.lexilogos.com
any idea?
Upvotes: 0
Views: 61
Reputation: 89557
I suggest to use the find
method instead of the matches
that must check all the string. I rewrite your pattern to limit the amount of backtracking:
String urlPattern = "\\bhttps?://[^h]*+(?:(?:\\Bh|h(?!ttps?://))[^h]*)*+https?://";
Pattern p = Pattern.compile(urlPattern, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
if (m.find()) {
// true
} else {
// false
}
pattern details:
\\b # word boundary
https?:// # scheme for http or https
[^h]*+ # all that is not an "h"
(?:
(?:
\\Bh # an "h" not preceded by a word boundary
| # OR
h(?!ttps?://) # an "h" not followed by "ttp://" or "ttps://"
)
[^h]*
)*+
https?:// # an other scheme
Upvotes: 1
Reputation: 21902
Just use this to count how many links you have:
private static int countLinks(String str) {
int total = 0;
Pattern p = Pattern.compile("(?:http|https):\\/\\/");
Matcher m = p.matcher(str);
while (m.find()) {
total++;
}
return total;
}
Then
boolean hasMoreThanTwo = countLinks("They say 2's company watch live on...? http://www.le testin this code http://www.lexilogos.com") >= 2;
If you just want to know if you have two or more, just exit after you found two.
Upvotes: 2