Extract All Unique Words using regex - Java Regular Expressions

Java examples for Regular Expressions:Word

Description

Extract All Unique Words using regex

Demo Code

     //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  + " ");
            }

        }

    }

Related Tutorials