英語の単語の数を統計する

461 ワード

正規表現を使用して、テキスト内の英語の単語の数を統計します.

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