Java Program to Find Occurrence of a Word Using Regex Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Java's regular expressions, or regex, let you do advanced text manipulation and matching. Regex offers a handy approach for searching for a term in a text wherever it appears. In this article, we will learn to find every occurrence of a word using regex. Program to Find Occurrence of a Word Using RegexThe primary idea is to use Java's java.util.regex library, namely the Pattern and Matcher classes. You may create a pattern that matches a certain word or character sequence using regular expressions. The Matcher class assists in locating instances of the pattern within a supplied text, while the Pattern class assembles the regex pattern. Below is the implementation of finding the occurrence of a word using Regex: Java // Java program to find occurrences of a // specific word in a given text using regular expressions import java.util.regex.Matcher; import java.util.regex.Pattern; // Class definition for WordOccurrencesExample public class WordOccurrencesExample { // Main method public static void main(String[] args) { // Create a sample text String text = "Java is a versatile programming language. Java is widely used in software development."; // Define the word to find occurrences String wordToFind = "Java"; // Create a regex pattern using the word Pattern pattern = Pattern.compile("\\b" + wordToFind + "\\b", Pattern.CASE_INSENSITIVE); // Create a matcher for the text Matcher matcher = pattern.matcher(text); // Find and display every occurrence of the word System.out.println("Occurrences of the word '" + wordToFind + "':"); while (matcher.find()) { System.out.println("Found at index " + matcher.start() + " - " + matcher.group()); } } } OutputOccurrences of the word 'Java': Found at index 0 - Java Found at index 42 - Java Explaination of the above Program: Create a sample text Define the word to find occurrences Create a regex pattern using the word Create a matcher for the text Find and display every occurrence of the word Comment More infoAdvertise with us Next Article Java Program to Find Occurrence of a Word Using Regex R ravi86526iv Follow Improve Article Tags : Java Java Programs Java-Strings java-regular-expression regular-expression Java Examples +2 More Practice Tags : JavaJava-Strings Similar Reads Java Program to Find the Occurrence of Words in a String using HashMap HashMap<Key, Value> provides the basic implementation of the Map interface of Java and import java.util.HashMap package or its superclass. HashMap stores the data in (Key, Value) pairs, and accessed by an index of another type (e.g. an Integer). One object is used as a key to another object. I 3 min read Java Program to Search a Particular Word in a String Using Regex In Java string manipulation, searching for specific words is a fundamental task. Regular expressions (regex) offer a powerful and flexible approach to achieve this search. It matches the patterns simply by comparing substrings. In this article, we will learn how to search for a particular word in a 3 min read How to Replace the First Occurrence of a String Using Regex in Java? Regex is a very interesting way to search for patterns in a String that the user provides. Regex stands for Regular Expressions. It consists of some patterns that can be planned and modified according to the usage of the program. In this article, we will discuss how to replace the first occurrence o 2 min read How to Replace All Occurings of String Using Regex in Java? Regex in Java is an interesting way to search for patterns in a string that the user provides. Expanded as Regular Expressions, It consists of some patterns that can be planned and modified according to the usage in the program. Example of Replace All Occurings of a StringInput: str="This is a sampl 2 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Java Program to Print all Unique Words of a String Java program to print all unique words present in the string. The task is to print all words occurring only once in the string. Illustration: Input : Welcome to Geeks for Geeks. Output : Welcome to for Input : Java is great.Python is also great. Output : Java Python also Methods: This can be done in 4 min read Count Occurrences of a Given Character using Regex in Java Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in 2 min read Java Program to Find the Most Repeated Word in a Text File Map and Map.Entry interface will be used as the Map interface maps unique keys to values. A key is an object that is used to retrieve a value at a later date. The Map.Entry interface enables you to work with a map entry. Also, we will use the HashMap class to store items in "key/valueâ pairs and acc 3 min read How to find the last index using regex of a particular word in a String? To find the last index of a particular word in a string using regular expressions in Java, you can use the Matcher class along with a regular expression that captures the desired word. Java Program to last index Using the Regex of a Particular Word in a StringBelow is the implementation of the last 2 min read 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 4 min read Like