Java URL counter is not working

2019-08-29 13:33发布

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);

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

标签: java regex url
1条回答
够拽才男人
2楼-- · 2019-08-29 14:06

From the javadoc for Matcher#groupCount()

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

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

查看更多
登录 后发表回答