Sunday, October 5, 2025
HomeLanguagesJavaMatcher replaceFirst(Function) method in Java with Examples

Matcher replaceFirst(Function) method in Java with Examples

The replaceFirst(Function) method of Matcher Class behaves as a append-and-replace method. This method replaces first instance of the pattern matched in the matcher with the function passed in the parameter.

Syntax:

public String replaceFirst(
        Function replacerFunction)

Parameters: This method takes a parameter replacerFunction which is the function that defines the replacement of the matcher.

Return Value: This method returns a String with the target String constructed by replacing the String.

Exceptions: This method throws following exceptions:

  • NullPointerException: if the replacer function is null
  • ConcurrentModificationException: if it is detected that the replacer function modified this matcher’s state

Below examples illustrate the Matcher.replaceFirst() method:

Example 1:




// Java code to illustrate replaceFirst() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(Geeks)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks Geeks for For Geeks Geek";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
  
        // Get the String to be replaced
        String stringToBeReplaced = "Geeks";
        StringBuilder builder
            = new StringBuilder();
  
        // Replace every matched pattern
        // with the target String
        // using replaceFirst() method
        System.out.println("After Replacement: "
                           + matcher
                                 .replaceFirst(
                                     x -> x.group().toUpperCase()));
    }
}


Output:

Before Replacement: GeeksForGeeks Geeks for For Geeks Geek
After Replacement: GEEKSForGeeks Geeks for For Geeks Geek

Example 2:




// Java code to illustrate replaceFirst() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(FGF)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GFG FGF GFG GFG FGF";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
  
        // Get the String to be replaced
        String stringToBeReplaced = "(GFG)";
        StringBuilder builder
            = new StringBuilder();
  
        // Replace every matched pattern
        // with the target String
        // using replaceFirst() method
        System.out.println("After Replacement: "
                           + matcher
                                 .replaceFirst(
                                     x -> x.group().toLowerCase()));
    }
}


Output:

Before Replacement: GFG FGF GFG GFG FGF
After Replacement: GFG fgf GFG GFG FGF

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#replaceFirst-java.util.function.Function-

RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6823 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS