Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Extracting each word from a String using Regex in Java

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Last Updated : 11 Dec, 2018
Like Article
Save Article
Similar Reads
Related Tutorials