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.