Java RegEx Matcher.groupCount 返回 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12413974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java RegEx Matcher.groupCount returns 0
提问by David Homes
I know this has been asked but I am unable to fix it
我知道有人问过这个问题,但我无法修复它
For a book object with body (spanish): "quiero mas dinero"
(actually quite a bit longer)
对于带有正文的书对象(西班牙语):("quiero mas dinero"
实际上要长一些)
My Matcher
keeps returning 0 for:
我Matcher
不断返回 0 为:
String s="mas"; // this is for testing, comes from a List<String>
int hit=0;
Pattern p=Pattern.compile(s,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(mybooks.get(i).getBody());
m.find();
System.out.println(s+" "+m.groupCount()+" " +mybooks.get(i).getBody());
hit+=m.groupCount();
I keep getting "mas 0 quiero mas dinero"
on console. Why oh why?
我一直"mas 0 quiero mas dinero"
在控制台上。为什么哦为什么?
采纳答案by Keppil
From the javadoc of Matcher.groupCount():
来自Matcher.groupCount()的 javadoc :
Returns the number of capturing groups in this matcher's pattern.
Group zero denotes the entire pattern by convention. It is not included in this count.
返回此匹配器模式中的捕获组数。
按照惯例,组零表示整个模式。它不包括在此计数中。
If you check the return value from m.find()
it returns true
, and m.group()
returns mas
, so the matcher does find a match.
如果您检查它的返回值,m.find()
则返回true
, 并m.group()
返回mas
,因此匹配器确实找到了匹配项。
If what you are trying to do is to count the number of occurances of s
in mybooks.get(i).getBody()
, you can do it like this:
如果您想要做的是计算s
in的出现次数mybooks.get(i).getBody()
,您可以这样做:
String s="mas"; // this is for testing, comes from a List<String>
int hit=0;
Pattern p=Pattern.compile(s,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(mybooks.get(i).getBody());
while (m.find()) {
hit++;
}
回答by Reimeus
How could I then find the number of "mas" (or any other) words in a string without looping?
那么我如何在不循环的情况下找到字符串中“mas”(或任何其他)单词的数量?
You could use StringUtilsin Apache Commons:
您可以在 Apache Commons 中使用StringUtils:
int countMatches = StringUtils.countMatches("quiero mas dinero...", "mas");
回答by jinglezju
You can add parenthesis in the regExp, then it is "(mas)" in your example.
您可以在正则表达式中添加括号,然后在您的示例中为“(mas)”。
回答by R. Sham
You can add parenthesis in the regExp, then it is "(mas)" in your example.
您可以在正则表达式中添加括号,然后在您的示例中为“(mas)”。
That way is not suitable for this task. It shows number of capturing groups contain result of Matcher m. In this case even if pattern is "(mas)" for input text like "mas mas" m.groupcount() show 1 - one and only groop for both matches.
这种方式不适合这项任务。它显示了包含匹配器 m 结果的捕获组的数量。在这种情况下,即使输入文本的模式为“(mas)”,如“mas mas” m.groupcount() 显示 1 - 两个匹配项的一个且唯一的组。
So first response is correct and the only possible for the purpose of matches counting.
因此,第一个响应是正确的,并且是匹配计数的唯一可能。