matcher.group()必须和matcher.find()配合才行,不然Java语言会报运行时异常。
通过在循环中执行matcher.group(),可以将所有匹配项全部提取出来,因此在处理字符串时非常有用。
下面代码是它的一个demo,我把它记录下来方便以后使用。如果有帮助到你,请点个赞!
public class Finding {
public static void main(String[] args) {
Matcher matcher = Pattern.compile("\\w+")
.matcher("Evening is full of the linnet's wings");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
执行结果如下
Evening
is
full
of
the
linnet
s
wings
参考文献:
佚名. Java编程思想[M]. 2007.
版权声明:本文为github_37412255原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。