Matcher.replaceAll() / replaceFirst()
Methods for replacing substrings that match a regular expression. replaceAll() replaces all matches, while replaceFirst() replaces only the first match.
Syntax
// Replaces all matches with the specified string. String result = matcher.replaceAll(String replacement); // Replaces only the first match. String result = matcher.replaceFirst(String replacement); // Builds a replacement by processing each match individually. matcher.appendReplacement(StringBuffer sb, String replacement); // Appends the remaining string to the StringBuffer. matcher.appendTail(StringBuffer sb);
Method List
| Method | Description |
|---|---|
| replaceAll(String replacement) | Returns a new string with all substrings matching the pattern replaced by replacement. |
| replaceFirst(String replacement) | Replaces only the first substring that matches the pattern. |
| appendReplacement(StringBuffer sb, String replacement) | Appends to sb the text between the previous match end and the current match start, followed by the replacement string. |
| appendTail(StringBuffer sb) | Appends the remaining text after the last match to sb. |
| replaceAll(Function<MatchResult,String> replacer) | Java 9+. Dynamically generates replacement strings using a function that receives each match result. |
Sample Code
import java.util.regex.*;
// Use replaceAll() to replace all digit sequences with "*".
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("Phone: 090-1234-5678");
System.out.println(m.replaceAll("*")); // Prints "Phone: *-*-*".
// Use replaceFirst() to replace only the first match.
Matcher m2 = p.matcher("Value is 3, count is 5.");
System.out.println(m2.replaceFirst("N")); // Prints "Value is N, count is 5.".
// Use appendReplacement() and appendTail() for dynamic replacement.
Pattern wordPattern = Pattern.compile("\\b(Java|Python)\\b");
Matcher wordMatcher = wordPattern.matcher("Java and Python are popular languages.");
StringBuffer sb = new StringBuffer();
while (wordMatcher.find()) {
wordMatcher.appendReplacement(sb, "[" + wordMatcher.group() + "]");
}
wordMatcher.appendTail(sb);
System.out.println(sb.toString()); // Prints "[Java] and [Python] are popular languages.".
// Java 9+: Use replaceAll() with a lambda for dynamic replacement.
Matcher m3 = Pattern.compile("\\d+").matcher("Value is 3, count is 12.");
String result = m3.replaceAll(mr -> String.valueOf(Integer.parseInt(mr.group()) * 2));
System.out.println(result); // Prints "Value is 6, count is 24.".
Notes
replaceAll() and replaceFirst() are methods on a Matcher object that replace the portions of the input that match the regular expression with a specified string. The replacement string can reference capture groups using $1, $2, and so on.
Combining appendReplacement() and appendTail() lets you generate different replacement content for each match using custom logic. If the replacement string contains $ or \, you must escape it with Matcher.quoteReplacement().
For pattern compilation and basic matching, see 'Pattern.compile() / Matcher.matches() / find()'.
If you find any errors or copyright issues, please contact us.