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!
From the javadoc for
Matcher#groupCount()
Your pattern has one group
...(https?|ftp|file)...
so it returns 1.