Java写爬虫的时候,matcher.groupCount()返回为1,但是matcher

https://img2.mukewang.com/5cc12899000174e608000182.jpg

撒科打诨
浏览 676回答 1
1回答

慕森卡

我模仿了题意,写了测试代码,结果如下String html = "<p><span>mytextvalue<br>";Matcher m = Pattern.compile("<p><span>(.*?)<br>").matcher(html);System.out.println(m.find()); //trueSystem.out.println(m.groupCount()); //1System.out.println(m.group(0)); //<p><span>mytextvalue<br>System.out.println(m.group(1)); //mytextvalue另外// where does m.groupCount come fromm = Pattern.compile("(group1)(group2)(group3)").matcher(html);System.out.println(m.groupCount()); //3增加解释说明,看源码的注释&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Returns the number of capturing groups in this matcher's pattern.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* <p> Group zero denotes the entire pattern by convention. It is not&nbsp; &nbsp; &nbsp;* included in this count.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* <p> Any non-negative integer smaller than or equal to the value&nbsp; &nbsp; &nbsp;* returned by this method is guaranteed to be a valid group index for&nbsp; &nbsp; &nbsp;* this matcher.&nbsp; </p>&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return The number of capturing groups in this matcher's pattern&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public int groupCount() {&nbsp; &nbsp; &nbsp; &nbsp; return parentPattern.capturingGroupCount - 1;&nbsp; &nbsp; }这里说得清楚,groupCount返回的是正则表达式的捕获分组的数量(捕获分组和非捕获分组是另外的知识点),groupCount的结果并不能说明匹配的结果。要执行正则表达式匹配,需要执行find动作,看源码&nbsp; &nbsp; public boolean find() {&nbsp; &nbsp; &nbsp; &nbsp; int nextSearchIndex = last;&nbsp; &nbsp; &nbsp; &nbsp; if (nextSearchIndex == first)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextSearchIndex++;&nbsp; &nbsp; &nbsp; &nbsp; // If next search starts before region, start it at region&nbsp; &nbsp; &nbsp; &nbsp; if (nextSearchIndex < from)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextSearchIndex = from;&nbsp; &nbsp; &nbsp; &nbsp; // If next search starts beyond region then it fails&nbsp; &nbsp; &nbsp; &nbsp; if (nextSearchIndex > to) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < groups.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groups[i] = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return search(nextSearchIndex);&nbsp; &nbsp; }这样的才会给Matcher内部的成员变量groups赋值,groups[i] = -1;这样的之后在我们执行m.group(1)的时候我们才能获得捕获分组匹配到的内容。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java