使用正则表达式来统计一段文字中的英文单词的数量
结果:count=3
public static void main(String[] args) throws IOException {
String str = "Welcome to China.";
Pattern pattern = Pattern.compile("\\b\\w+\\b");
Matcher matcher = pattern.matcher(str);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println("count=" + count);
}
结果:count=3