How to Replace All Occurings of String Using Regex in Java? Last Updated : 31 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 sample string and it doesn't exist in real world. Refer to GFG for real String related data"; to_Replace="string" final_element="....." Output: This is a sample ..... and it doesn't exist in real world. Refer to GFG for real ..... related data Java Program to Replace all Occurrence of StringBelow is the implementation of Replace all Occurrence of String : Java // Java Program to Replace // all Occurrence of String 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 = "I am A Normal Person and a normal human being"; String replacementString = "Extraordinary"; String original_element="normal"; // String that is to be replaced String searchString = "\\b(?i)" + original_element + "\\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.replaceAll(replacementString); System.out.println("Original String: " + inputString); System.out.println("Result String: " + resultString); } } OutputOriginal String: I am A Normal Person and a normal human being Result String: I am A Extraordinary Person and a Extraordinary human being Explanation of the above code:To replace all the specified word in the string with given replacement String , we make use of the regex pattern matcher. First we have to provide the 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 occurance with the replaceAll() method.Note: (?i) ignores the case and \\b is used for word boundaries to ensure that only whole words are matches Comment More infoAdvertise with us Next Article How to Replace All Occurings of String Using Regex in Java? T tmpravi5i5j Follow Improve Article Tags : Java Java Programs Java-Strings java-regular-expression regular-expression Java Examples +2 More Practice Tags : JavaJava-Strings Similar Reads 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 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 How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 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 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 Like