Matcher replaceFirst(String) Method in Java with Examples - GeeksforGeeks

10 Nov, 2021
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 classesimport java.util.regex.*; // Main classpublic 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));    }}
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 classesimport java.util.regex.*; // Main classpublic 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));    }}
Before Replacement: FGF FGF FGF FGF
After Replacement: GFG FGF FGF FGF
 
Akanksha_Rai
solankimayank
Java - util package
Java-Functions
Java-Matcher
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Internal Working of HashMap in Java
Checked vs Unchecked Exceptions in Java
Strings in Java
StringBuilder Class in Java with Examples