Java URL 计数器不起作用 [英] Java URL counter is not working

查看:24
本文介绍了Java URL 计数器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to count the number of URLs in any given Java string:

String test = "Hello World!";
String urlRegex = "<\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]>";
pattern = Pattern.compile(urlRegex);
matcher = pattern.matcher(test);
numUrls = matcher.groupCount();
System.err.println("numUrls = " + numUrls);

我很惊讶地看到 numUrls 不是零.关于为什么的任何想法?提前致谢!

I am surprised to see that numUrls is not zero. Any ideas as to why? Thanks in advance!

推荐答案

来自 Matcher#groupCount()

返回此匹配器模式中捕获组的数量.

Returns the number of capturing groups in this matcher's pattern.

你的模式有一组 ...(https?|ftp|file)... 所以它返回 1.

Your pattern has one group ...(https?|ftp|file)... so it returns 1.

这篇关于Java URL 计数器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆