The appendTail(StringBuffer) method of the Matcher class is used to append the remaining portion of the input string to a StringBuffer after all replacements have been performed using the appendReplacement() method. It is typically used together with appendReplacement() to perform custom find-and-replace operations while preserving the unmatched part of the input string.
- Appends all remaining characters after the last regex match.
- Completes the replacement process started with appendReplacement().
- Preserves the unmatched portion of the input string.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Geeks {
public static void main(String[] args) {
String input = "The cat chased another cat in the garden.";
String regex = "cat";
String replacement = "dog";
// Compile the regular expression
Pattern pattern = Pattern.compile(regex);
// Create a Matcher object
Matcher matcher = pattern.matcher(input);
StringBuffer result = new StringBuffer();
// Replace each matched word
while (matcher.find()) {
matcher.appendReplacement(result, replacement);
}
// Append the remaining text
matcher.appendTail(result);
System.out.println("Original String : " + input);
System.out.println("Modified String : " + result);
}
}
Output
Original String : The cat chased another cat in the garden. Modified String : The dog chased another dog in the garden.
Explanation: The regular expression "cat" matches every occurrence of the word cat. The appendReplacement() method replaces each match with dog, while appendTail() appends the remaining unmatched text (" in the garden.") after the final replacement, producing the complete modified string.
Syntax
public StringBuffer appendTail(StringBuffer buffer)
Parameter
- buffer: The
StringBufferto which the remaining part of the input string is appended.
Return Value: Returns the same StringBuffer after appending the remaining characters of the input sequence.
Example: Replacing Words Using appendTail()
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Geeks {
public static void main(String[] args) {
String input = "Java is powerful. Java is platform independent.";
String regex = "Java";
String replacement = "JAVA";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
StringBuffer result = new StringBuffer();
// Replace each match
while (matcher.find()) {
matcher.appendReplacement(result, replacement);
}
// Append remaining text
matcher.appendTail(result);
System.out.println("Original : " + input);
System.out.println("Modified : " + result);
}
}
Output
Original : Java is powerful. Java is platform independent. Modified : JAVA is powerful. JAVA is platform independent.
Explanation: The appendReplacement() method replaces each occurrence of "Java" with "JAVA". After all replacements are completed, appendTail() appends the remaining unmatched text (" is platform independent.") to the StringBuffer, producing the final modified string.
Example: Replacing Numbers with Text
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Geeks {
public static void main(String[] args) {
String input = "Item1 costs 100 and Item2 costs 250.";
String regex = "\\d+";
String replacement = "NUMBER";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(result, replacement);
}
matcher.appendTail(result);
System.out.println("Original : " + input);
System.out.println("Modified : " + result);
}
}
Output
Original : Item1 costs 100 and Item2 costs 250. Modified : ItemNUMBER costs NUMBER and ItemNUMBER costs NUMBER.
Explanation: The regular expression \\d+ matches one or more digits. Each matched number is replaced with "NUMBER" using appendReplacement(). Finally, appendTail() appends the remaining part of the input string after the last match.
Difference Between appendReplacement() and appendTail()
| Method | Purpose |
|---|---|
| appendReplacement() | Replaces the current matched substring and appends text before the match. |
| appendTail() | Appends the remaining unmatched part of the input string after all matches are processed. |
Advantages of appendTail() Method
- Preserves the remaining unmatched text automatically.
- Completes custom regex replacement operations.
- Works seamlessly with appendReplacement().
- Efficient for processing multiple regex matches.
- Eliminates the need for manual string concatenation.