[Solved] Extract words from string with regex

  

3
Topic starter

Write a program that extracts words from a string. Words are sequences of characters that are at least two symbols long and consist only of English alphabet letters. Use regex.

Examples:

extract words in java task

1 Answer
2

Interesting task! For the Java regex, I am using this site: http://regexr.com/

Anyways, here's my solution:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Pr_05_ExtractWords {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        String input = scanner.nextLine();
 
        Pattern pattern = Pattern.compile("([a-zA-Z]+)");
        Matcher matcher = pattern.matcher(input);
 
        while (matcher.find()) {
            System.out.print(matcher.group() + " ");
        }
    }
}
Share: