How to Replace the First Occurrence of a String Using Regex in Java? Last Updated : 02 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 of a String using regex in Java. Example to Replace the First Occurrence of StringInput: str = "Hello World . World is beautiful" element = "World" replace_element= "GFG" Output: Hello GFG . World is beautiful Java Program to replace the first occurrence of a String using regexBelow is the code implementation to replace the first occurrence of a String using regex: Java // Java Program to replace the first occurrence of a String using regex import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; // Driver Class class GFG { // Main Function public static void main(String[] args) { String inputString = "She is A Good Person . A good person is not always extraordinary"; String replacementString = "Extraordinary"; // String that is to be replaced String searchString = "\\b(?i)good\\b"; Pattern regex = Pattern.compile(searchString); // Create a matcher for the input // string Matcher matcher = regex.matcher(inputString); // Use replaceAll to replace all //occurrences of the Regex String resultString = matcher.replaceFirst(replacementString); System.out.println("Original String: " + inputString); System.out.println("Result String: " + resultString); } } OutputOriginal String: She is A Good Person . A good person is not always extraordinary Result String: She is A Extraordinary Person . A good person is not always extraordinary Explanation of the above Program:To replace the first occurance in the string with given replacement String, we make use of the regex pattern matcher. First, we have to provide a pattern that matches exactly with the Input String. Then With the Pattern.compile() method we compile the regex pattern to a String. Then matcher function is used to extract the matched word from the Input String.After obtaining the matched word It is much easier to replace all its first with the replaceFirst() method. Comment More infoAdvertise with us Next Article How to Replace the First Occurrence of a String Using Regex in Java? T tmpravi5i5j Follow Improve Article Tags : Java Java Programs Java-Strings java-regular-expression Java Examples +1 More Practice Tags : JavaJava-Strings Similar Reads 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 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 How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read Capitalize the First Letter of a String in Java Using Regex In Java, we can use regular expressions to make the first character of each word in Uppercase as the word is represented as a String and string is the collection of characters. So, in Java, we can use pattern and matcher class methods to find the first character of a word and then replace it with it 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