How to Replace All Occurings of String Using Regex in Java? Last Updated : 23 Jul, 2025 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 info T tmpravi5i5j Follow Improve Article Tags : Java Java Programs Java-Strings java-regular-expression regular-expression Java Examples +2 More Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like