计算正则表达式的匹配

需要计算正则表达式发现声明数组的匹配,我试过.groupCount()但是没有实现。

代码如下:

Pattern p2 = Pattern.compile("<char>(.*?)</char>");
response = response.replaceAll("[\\r\\n]+",  "");
Matcher m2 = p2.matcher(response);

String[] chars = new String[m2.groupCount()];

应该找到2,但是返回的是1。

不知道为什么。

groupCount反回的不是你的字符串从中匹配了多少个,而是正则表达式的匹配的组数(就是<char>(.*?)</char>是正则匹配的个数)。 

try: p2.matcher(response).count