The replaceFirst(String) method of the Matcher class is used to replace only the first substring that matches a regular expression with a specified replacement string. Unlike replaceAll(), it stops after replacing the first match and returns a new string.
- Replaces only the first occurrence of the matched pattern.
- Returns a new string while leaving the original string unchanged.
- Useful when only the first matching substring needs to be updated.
Example: Replacing the First Occurrence of a Word
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
// Input string
String text = "Java is easy. Java is powerful.";
// Create pattern and matcher
Pattern pattern = Pattern.compile("Java");
Matcher matcher = pattern.matcher(text);
System.out.println("Before Replacement: " + text);
// Replace the first occurrence
String result = matcher.replaceFirst("Python");
System.out.println("After Replacement: " + result);
}
}
Output
Before Replacement: Java is easy. Java is powerful. After Replacement: Python is easy. Java is powerful.
Explanation: The regular expression matches the word "Java". The replaceFirst() method replaces only the first occurrence with "Python", while the second occurrence remains unchanged.
Syntax
public String replaceFirst(String replacement)
- 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: Replacing the First Number
import java.util.regex.*;
public class Geeks {
public static void main(String[] args) {
// Input string
String text = "Order123 and Order456 are ready.";
// Create pattern and matcher
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(text);
System.out.println("Before Replacement: " + text);
// Replace the first sequence of digits
String result = matcher.replaceFirst("###");
System.out.println("After Replacement: " + result);
}
}
Output
Before Replacement: Order123 and Order456 are ready. After Replacement: Order### and Order456 are ready.
Explanation: The regular expression \\d+ matches one or more digits. The replaceFirst() method replaces only the first matched number (123) with ###, leaving the second number (456) unchanged.
Difference Between replaceFirst() and replaceAll()
| Feature | replaceFirst() | replaceAll() |
|---|---|---|
| Number of replacements | Replaces only the first match | Replaces all matches |
| Remaining matches | Left unchanged | All matching substrings are replaced |
| Return value | Returns a new string | Returns a new string |
| Original string | Not modified | Not modified |
| Best use case | When only the first occurrence needs to be updated | When every occurrence needs to be updated |
| Performance | Slightly faster when only the first match is required | May take longer because it scans the entire input |
| Example | "Java Java".replaceFirst("Java","Python") → "Python Java" | "Java Java".replaceAll("Java","Python") → "Python Python" |
Advantages of replaceFirst(String) Method
- Replaces only the first matching substring.
- Returns a new string without modifying the original string.
- Supports regular expressions for flexible pattern matching.
- Useful for updating only the first occurrence in a string.
- Simple and efficient for one-time replacement operations.