Matcher replaceFirst(String) Method in Java with Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The replaceFirst() method of Matcher Class behaves as an append-and-replace method. This method reads the input string and replaces it with the first matched pattern in the matcher string. Syntax: public String replaceFirst(String stringToBeReplaced) Parameters: The string to be replaced that is the String to be replaced in the matcher. Return Type: A string with the target string constructed by replacing the string. Example 1: Java // Java Program to Illustrate replaceFirst() Method // of Matcher class // Importing required classes import java.util.regex.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Getting the regex to be checked String regex = "(Geeks)"; // Creating a pattern from regex Pattern pattern = Pattern.compile(regex); // Getting the String to be matched String stringToBeMatched = "GeeksForGeeks Geeks for For Geeks Geek"; // Creating a matcher for the input String Matcher matcher = pattern.matcher(stringToBeMatched); // Displaying the string to be matched // before replacing System.out.println("Before Replacement: " + stringToBeMatched); // Getting the string to be replaced String stringToBeReplaced = "GFG"; StringBuilder builder = new StringBuilder(); // Replacing every matched pattern with the target // string using replaceFirst() method System.out.println( "After Replacement: " + matcher.replaceFirst(stringToBeReplaced)); } } Output: Before Replacement: GeeksForGeeks Geeks for For Geeks Geek After Replacement: GFGForGeeks Geeks for For Geeks Geek Example 2: Java // Java Program to Illustrate replaceFirst() Method // Importing required classes import java.util.regex.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Getting the regex to be checked String regex = "(FGF)"; // Creating a pattern from regex Pattern pattern = Pattern.compile(regex); // Getting the string to be matched String stringToBeMatched = "FGF FGF FGF FGF"; // Creating a matcher for the input String Matcher matcher = pattern.matcher(stringToBeMatched); // Printing string on console // before replacement System.out.println("Before Replacement: " + stringToBeMatched); // Getting the string to be replaced String stringToBeReplaced = "GFG"; StringBuilder builder = new StringBuilder(); // Replacing every matched pattern with target // string using replaceFirst() method and printing // the string to be replaced System.out.println( "After Replacement: " + matcher.replaceFirst(stringToBeReplaced)); } } Output: Before Replacement: FGF FGF FGF FGF After Replacement: GFG FGF FGF FGF Create Quiz Comment K Kirti_Mangal Follow 0 Improve K Kirti_Mangal Follow 0 Improve Article Tags : Java Java - util package Java-Functions Java-Matcher Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 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 Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like