简体   繁体   English

检查文本是否有多个链接

[英]check if the text has more than one link

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? 任何想法?

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. 如果您只想知道是否有两个或两个以上,则在找到两个之后退出。

I suggest to use the find method instead of the matches that must check all the string. 我建议使用find方法,而不是必须检查所有字符串的matches 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

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

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