How to find the last index using regex of a particular word in a String? Last Updated : 25 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 index Using the Regex of a Particular Word in a String: Java // Java Program to Find last index // Using the Regex of a Particular Word in a String import java.util.regex.Matcher; import java.util.regex.Pattern; // Driver Class public class LastIndexOfWordUsingRegex { // Main Function public static void main(String[] args) { String inputString = "This is a sample sentence with a particular word. Another occurrence of the word."; // Specify the word you want to find String wordToFind = "word"; // Create a pattern with the word and the end-of-input anchor String regex = "\\b" + Pattern.quote(wordToFind) + "\\b"; Pattern pattern = Pattern.compile(regex); // Create a matcher for the input string Matcher matcher = pattern.matcher(inputString); // Find the last occurrence of the word int lastIndex = -1; while (matcher.find()) { lastIndex = matcher.start(); } // Print the last index or indicate if the word is not found if (lastIndex != -1) { System.out.println("Last index of '" + wordToFind + "': " + lastIndex); } else { System.out.println("'" + wordToFind + "' not found in the string."); } } } Output: Last index of 'word': 76Explanation of the above Program: The regular expression \\b is used to denote a word boundary.Pattern.quote(wordToFind) is used to escape any special characters in the word.The end-of-input anchor $ is used to ensure that the match occurs at the end of the input.The Matcher iterates through the input string using find(), updating the lastIndex each time a match is found.Note: This approach assumes that you want to match whole words. If you want to find occurrences within other words, you may need to adjust the regular expression accordingly. Comment More infoAdvertise with us Next Article Java Program to Search a Particular Word in a String Using Regex A akhilmaurya Follow Improve Article Tags : Java Java Programs java-regular-expression regular-expression Java Examples +1 More Practice Tags : Java Similar Reads Java Program to find the Last Index of a Particular Word in a String To find out the last occurrence of a character or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found. Real-life Example: Consider an example of a book. The book contains pages where pages conta 4 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 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 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 the Last Occurrence of a Substring in a String in Java? In this article, we will learn about replacing the last instance of a certain substring inside a string as a typical need. We'll look at a practical Java solution for this in this post. Replace the last occurrence of a Substring in a String in JavaWe may use the lastIndexOf() method to determine the 2 min read Java Program to Find Occurrence of a Word Using Regex 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 Re 2 min read Like