Java is a language of choice for millions of developers worldwide. Javaは、言語の選択を何百万もの開発者の世界です。 In a series of articles I will show simple tips and techniques which make Java extremely powerful and yet simple to use.で、一連の記事が表示さ簡単なヒントやテクニックを私にJavaの非常に強力ではまだありませんが、簡単に使用します。 Today’s article is about using regex, a pattern matcher incorporated in Java (from 1.4 I believe).今日の記事は正規表現を使用して、 Javaのパターンが組み込まれてmatcher (より1.4と思う) 。

Here is a sample code (line in bold) to count the number of words in any amount of text.次に示すのは、サンプルコード(太字で表示さ行)の単語数をカウントするためにどれだけの量のテキストです。 The sample program counts the number of words in the argument to the program.サンプルプログラムの数を数えるの単語を引数としてのプログラムです。 The argument must be quoted to ensure separate words are clubbed together in a single sentence by the operating system.引数を確保するための別の言葉を引用する必要がありますが、 1つの文章をこん棒で一緒には、オペレーティングシステムです。

 public class WordCount {   public static void main(String args[]) { System.out.println(java.util.regex.Pattern.compile(”[\\w]+”).split(args[0].trim()).length); } }パブリッククラスワードカウント(公共の静的な無効メイン(文字列args [ ] ) ( system.out.println ( java.util.regex.pattern.compile ( " [ \ \ワット]+").分割( args [ 0 ] 。トリム( ) ) 。長さ) ; ) ) 

Sample usage:サンプルの利用方法:
java WordCount “This is a sample phrase with 8 words” ジャワワードカウント"これは、サンプルのフレーズを8ワード"

All its does is compile the regular expression “\w” which matches with words and splits the phrase in an array of individual words.すべての機能は、正規表現をコンパイルして" \ w "のどの試合を単語やフレーズを分割し、個々の単語の配列をします。 Then the total length of the array is printed.の合計の長さを入力し、その配列が印刷されます。