How to extract each (English) word from a string using regular expression in Java?

JavaObject Oriented ProgrammingProgramming

The regular expression “[a-zA-Z]+” matches one or the English alphabet. Therefore, to extract each word in the given input string −

  • Compile the above expression of the compile() method of the Pattern class.

  • Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.

  • Finally, for each match get the matched characters by invoking the group() method.

Example

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EachWordExample {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter sample text: ");
      String data = sc.nextLine();
      String regex = "[a-zA-Z]+";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(data);
      System.out.println("Words in the given String: ");
      while(matcher.find()) {
         System.out.println(matcher.group()+" ");
      }
   }
}

Output

Enter sample text:
Hello this is a sample text
Words in the given String:
Hello
this
is
a
sample
text
raja
Published on 21-Nov-2019 12:27:02
Advertisements