84669 orang belajar
152542 orang belajar
20005 orang belajar
5487 orang belajar
7821 orang belajar
359900 orang belajar
3350 orang belajar
180660 orang belajar
48569 orang belajar
18603 orang belajar
40936 orang belajar
1549 orang belajar
1183 orang belajar
32909 orang belajar
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
我模仿了题意,写了测试代码,结果如下
String html = "mytextvalue"; Matcher m = Pattern.compile("(.*?)").matcher(html); System.out.println(m.find()); //true System.out.println(m.groupCount()); //1 System.out.println(m.group(0)); //mytextvalue System.out.println(m.group(1)); //mytextvalue
mytextvalue"; Matcher m = Pattern.compile("(.*?)").matcher(html); System.out.println(m.find()); //true System.out.println(m.groupCount()); //1 System.out.println(m.group(0)); //mytextvalue System.out.println(m.group(1)); //mytextvalue
(.*?)").matcher(html); System.out.println(m.find()); //true System.out.println(m.groupCount()); //1 System.out.println(m.group(0)); //mytextvalue System.out.println(m.group(1)); //mytextvalue
mytextvalue System.out.println(m.group(1)); //mytextvalue
另外
// where does m.groupCount come from m = Pattern.compile("(group1)(group2)(group3)").matcher(html); System.out.println(m.groupCount()); //3
增加解释说明,看源码的注释
/** * 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. * * Any non-negative integer smaller than or equal to the value * returned by this method is guaranteed to be a valid group index for * this matcher. * * @return The number of capturing groups in this matcher's pattern */ public int groupCount() { return parentPattern.capturingGroupCount - 1; }
Group zero denotes the entire pattern by convention. It is not * included in this count. * *
Any non-negative integer smaller than or equal to the value * returned by this method is guaranteed to be a valid group index for * this matcher.
这里说得清楚,groupCount返回的是正则表达式的捕获分组的数量(捕获分组和非捕获分组是另外的知识点),groupCount的结果并不能说明匹配的结果。
要执行正则表达式匹配,需要执行find动作,看源码
public boolean find() { int nextSearchIndex = last; if (nextSearchIndex == first) nextSearchIndex++; // If next search starts before region, start it at region if (nextSearchIndex to) { for (int i = 0; i 这样的才会给Matcher内部的成员变量groups赋值,groups[i] = -1;这样的之后在我们执行m.group(1)的时候我们才能获得捕获分组匹配到的内容。
这样的才会给Matcher内部的成员变量groups赋值,groups[i] = -1;这样的之后在我们执行m.group(1)的时候我们才能获得捕获分组匹配到的内容。
我模仿了题意,写了测试代码,结果如下
另外
增加解释说明,看源码的注释
这里说得清楚,groupCount返回的是正则表达式的捕获分组的数量(捕获分组和非捕获分组是另外的知识点),groupCount的结果并不能说明匹配的结果。
要执行正则表达式匹配,需要执行find动作,看源码