How to Replace All Occurings of String Using Regex in Java? Last Updated : 31 Jan, 2024 Comments Improve Suggest changes 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? 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 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 Remove all White Spaces from a String in Java? In Java, to remove all white spaces from a String, there are several ways like using replaceAll() or manual iteration over characters. In this article, we will learn how to remove all white spaces from a string in Java.Example:We will use the replaceAll() method with the regex pattern "\\s" to remov 3 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 Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 min read Java Program to Replace All Line Breaks from Strings Given a string, write a Java program to replace all line breaks from the given String. Examples Input - Geeks For Geeks Output - Geeks For Geeks Line Break: A line break ("\n") is a single character that defines the line change. In order to replace all line breaks from strings replace() function can 2 min read Like