Java Program to Search a Particular Word in a String Using Regex Last Updated : 16 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 string using regex in Java. Regular Expressions:Patterned expressions are used to represent sequences of characters and match them against text.Composed of special characters and metacharacters with specific meanings.Word Boundaries:Ensures that matches occur as complete words rather than parts of other words.Represented by \b in regex patterns.Step-by-Step ImplementationStep 1: Import java.util.regeximport java.util.regex.*;Step 2: Compile the Regex PatternCreate a Pattern object using Pattern.compile().Enclose the word you want to search for (e.g., "Java") within word boundaries \b.Optionally, use case-insensitive matching with Pattern.CASE_INSENSITIVE.String word = "Java"; // Word to search forPattern pattern = Pattern.compile("\\b" + word + "\\b", Pattern.CASE_INSENSITIVE);Step 3: Create a MatcherInstantiate a Matcher object using the compiled pattern and the string to search.find() attempts to find the first occurrence of the word.String text = "This is a string containing the word Java.";Matcher matcher = pattern.matcher(text);Step 4: Iterate and Process MatchesUse a loop to iterate through matches using find().Extract matched words:start() returns the index of the match's first character.end() returns the index after the last character (non-inclusive).Use substring extraction to get the matching word.Example with output formatting: while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String match = text.substring(start, end); System.out.println("Found word \"" + match + "\" at index [" + start + ", " + (end - 1) + "]");}Step 5: Handling No MatchesIf find() doesn't find any matches, inform the user or handle it within your logic.Program to Search a Particular Word in a String Using Regex in Java Java // Java program to search a particular word in a string using regex import java.util.regex.*; // Driver Class public class GFG { // Main Function public static void main(String[] args) { // word to search for String word = "Java"; String text = "This is a string containing the word Java. And another Java."; Pattern pattern = Pattern.compile( "\\b" + word + "\\b", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(text); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String match = text.substring(start, end); System.out.println("Found word \"" + match + "\" at index [" + start + ", " + (end - 1) + "]"); } } } OutputFound word "Java" at index [37, 40] Found word "Java" at index [55, 58] Comment More infoAdvertise with us Next Article Java Program to Remove a Given Word From a String H heysaiyad Follow Improve Article Tags : Java Java Programs Java-Strings java-regular-expression Java Examples +1 More Practice Tags : JavaJava-Strings Similar Reads 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 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 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 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 Remove a Given Word From a String Given a string and a word that task to remove the word from the string if it exists otherwise return -1 as an output. For more clarity go through the illustration given below as follows. Illustration: Input : This is the Geeks For Geeks word="the" Output : This is Geeks For Geeks Input : Hello world 3 min read Java Program to Extract an HTML Tag from a String using RegEx In this article, we will find/extract an HTML tag from a string with help of regular expressions. The Regular Expression Regex or Rational Expression is simply a character sequence that specifies a search pattern in a particular text. It can contain a single character or it can have a complex sequen 3 min read Like