0

我正在尝试计算 Java 字符串中的 URL 数量:

String test = "This http://example.com is a sentence https://secure.whatever.org that contains 2 URLs.";
String urlRegex = "<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>";
int numUrls = 0;
pattern = Pattern.compile(urlRegex);
matcher = pattern.matcher(test);
while(matcher.find())
    numUrls++;
System.err.println("numUrls = " + numUrls);

当我运行它时,它告诉我字符串中有零个(不是 2 个)URL。关于为什么的任何想法?提前致谢!

4

2 回答 2

5

<和中的>字符urlRegex导致您的模式和您的输入不匹配test String。删除它们将产生预期的numUrls值。2

于 2013-03-19T18:58:36.750 回答
0

试试这个代码:

        String data = "This http://example.com is a sentence https://secure.whatever.org that contains 2 URLs.";

    Pattern pattern = Pattern.compile("[hH][tT]{2}[Pp][sS]?://(\\w+(\\.\\w+?)?)+");
    Matcher matcher = pattern.matcher(data);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }

希望它会奏效。

于 2013-03-19T19:20:59.220 回答