settingsAccountsettings
Menusettings

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.

Examples:

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

 

answered by user sam
selected by user golearnweb
...