Open In App

Count a Group of Words in a String Using Regex in Java

Last Updated : 02 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Regular Expression is a powerful approach in Java for searching, Manipulating, and matching patterns with specific pattern requirements. In this article, we will learn to count a group of words in a string using regex.

First I explain count a group of words in a string using regex as per requirement, After that I will explain how to count all words in a given String by using regex.

Count a group of words

For this use case first I take one String variable with a value I take below the String value you can take another String value you want. After that we need to count a group of words means the group can contain certain boundaries like the starting position of a group of words and the ending position of a group of words. To count words in the group, we need to create a regex pattern for this logic.

Required Pattern

"\\b" + startingBoundary + "\\b\\s*(.*?)\\s*\\b" + endingBoundary + "\\b"

Explanation of Pattern:

  • \\b represents the boundary value in regex pattern
  • This startingBoundary variable represents the starting value of group
  • \s this represents the white spaces in the String value
  • (.*?) It can represents the capturing group for any characters in given String
  • After that again I set the ending boundary value by using endingBoundary variable

Java Program to Count group of words

Java




// Java Program to Extract Words Between Boundaries
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
// Driver Class
public class WordsBetweenBoundariesExample {
      // Main function
    public static void main(String[] args) {
  
        // Input string
        String inputString = "Welcome to GeeksForGeeks Best Online Platform for"
          + " learning Computer Science Subjects";
  
        // Starting and ending boundaries
        String startingBoundary = "Best";
        String endingBoundary = "Subjects";
  
        // Regular expression to capture words between boundaries
        String wordRegex = "\\b" + startingBoundary + "\\b\\s*(.*?)\\s*\\b" + endingBoundary + "\\b";
  
        // Create a Pattern object
        Pattern pattern = Pattern.compile(wordRegex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  
        // Create a Matcher object
        Matcher matcher = pattern.matcher(inputString);
  
        // Find the words between boundaries
        if (matcher.find()) {
            // Group 1 contains the words between boundaries
            String wordsBetweenBoundaries = matcher.group(1);
  
            // Split the captured words to count the number of words
            String[] wordsArray = wordsBetweenBoundaries.split("\\s+");
            int wordCount = wordsArray.length;
  
            // Display the input string
            System.out.println("\n\tInput string: " + inputString);
            System.out.println("\n\tStarting boundary Value: " + startingBoundary);
            System.out.println("\n\tEnding boundary Value: " + endingBoundary);
  
            // Display the result
            System.out.println("\n\tNumber of words between boundaries: " + wordCount);
            System.out.println("\n\tWords between boundaries: ");
            for (String word : wordsArray)
                System.out.print("\n\t" + word + ", ");
        
            
          else {
            // Display the input string
            System.out.println("\n\tInput string: " + inputString);
            System.out.println("\n\tStarting boundary Value: " + startingBoundary);
            System.out.println("\n\tEnding boundary Value: " + endingBoundary);
  
            // No match found
            System.out.println("\n\tNo match found between boundaries.");
        }
    }
}


Output

count1

Output while No match found between boundaries

count2

Explaination of the above Program:

In this above code first I import the required packages with required classes those Pattern and Matcher. After That I take One Sting value. After that I choose starting and ending boundary values from given string and assign them to two variables namely startingBoundary and endingBoundary. Now I will apply the regex pattern as per out requirement. After apply the regex we get some result. This result is stored in String type array. After that I used Pattern and Matcher classes for Matching the required pattern using Matcher class, then I count the size of that words using that array. Finally I print the result using print statement. If no words are found between given boundaries then we got message like No match found between boundaries. I will provide output images in the below for better understanding the concept with java code.

Count All Words

It is another use case for us, In this scenario we don’t provide any Starting and ending boundaries for pattern matching. And also we need change the regex also. Reaming is same as explained in the above and again I take String value but I change regex for this use case I will provide in the below.

Required Regex

"\\b\\w+\\b"


Example

In this example I write java code for count all the words from the given String and display the result.

Java




// Java Program to Count All Words in a String
  
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class AllWordCountExample {
    public static void main(String[] args) {
        // Input string
        String inputString = "Welcome to GeeksForGeeks Best Online Platform for" +
          " learning Computer Science Subjects";
  
        // Regular expression to match words
        String wordRegex = "\\b\\w+\\b";
  
        // Create a Pattern object
        Pattern pattern = Pattern.compile(wordRegex);
  
        // Create a Matcher object
        Matcher matcher = pattern.matcher(inputString);
  
        // Count the number of words
        int wordCount = 0;
        while (matcher.find()) {
            wordCount++;
        }
  
        // Display the result
        System.out.println("Number of words in the string: " + wordCount);
    }
}


Output

all



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads