Extracting each word from a String using Regex in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report Given a string, extract words from it. "Words" are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples: Input : Funny?? are not you? Output : Funny are not you Input : Geeks for geeks?? Output : Geeks for geeks We have discussed a solution for C++ in this post : Program to extract words from a given String We have also discussed basic approach for java in these posts : Counting number of lines, words, characters and paragraphs in a text file using Java and Print first letter in word using Regex. In this post, we will discuss Regular Expression approach for doing the same. This approach is best in terms of Time Complexity and is also used for large input files. Below is the regular expression for any word. [a-zA-Z]+ Java // Java program to demonstrate extracting words // from string using Regex import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String s1 = "Geeks for Geeks"; String s2 = "A Computer Science Portal for Geeks"; Pattern p = Pattern.compile("[a-zA-Z]+"); Matcher m1 = p.matcher(s1); Matcher m2 = p.matcher(s2); System.out.println("Words from string \"" + s1 + "\" : "); while (m1.find()) { System.out.println(m1.group()); } System.out.println("Words from string \"" + s2 + "\" : "); while (m2.find()) { System.out.println(m2.group()); } } } Output: Words from string "Geeks for Geeks" : Geeks for Geeks Words from string "A Computer Science Portal for Geeks" : A Computer Science Portal for Geeks Create Quiz Comment K kartik 2 Improve K kartik 2 Improve Article Tags : Java Java-Strings java-regular-expression Java-String-Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like