Java examples for Regular Expressions:Word
Extract All Unique Words using regex
//w w w . j a v a 2 s. c o m import java.util.Arrays; import java.util.Scanner; import java.util.TreeSet; public class ExtractAllUniqueWords { public static void main(String[] args) { Scanner input = new Scanner(System.in); String[] words = input.nextLine().toLowerCase().split("[\\W\\d]+"); TreeSet<String> uniqueWords = new TreeSet<>(Arrays.asList(words)); for (String word : uniqueWords) { System.out.print(word + " "); } } }