3
06/11/2024 7:04 am
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:
1 Answer
2
06/11/2024 7:05 am
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() + " "); } } }