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 w3wiki 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
Output while No match found between boundaries
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 a Group of Words in a String Using Regex in Java
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.
Contact Us