Q: Extract words from string with regex

+7 votes

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.


extract words in java task


asked in Java category by user richard8502
edited by user golearnweb

1 Answer

+2 votes
Best answer

Interesting task! For the Java regex, I am using this site:

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(;

        String input = scanner.nextLine();

        Pattern pattern = Pattern.compile("([a-zA-Z]+)");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.print( + " ");


answered by user sam
selected by user golearnweb