Phương thức appendReplacement() của lớp Matcher chấp nhận một đối tượng StringBuffer và một String (chuỗi thay thế) làm tham số, nối dữ liệu đầu vào vào StringBuffer, thay thế nội dung khớp bằng chuỗi thay thế.
Bên trong, phương thức này đọc từng ký tự từ chuỗi đầu vào và nối vào StringBuffer, khi có khớp xảy ra, nó nối chuỗi thay thế thay vì phần nội dung khớp của chuỗi vào buffer, và tiếp tục từ vị trí tiếp theo của chuỗi con khớp.
Khi truyền chuỗi thay thế cho phương thức này, nếu bạn dùng "/" hoặc "$", chúng sẽ không được coi là ký tự thông thường và một exception sẽ xảy ra:
Ví dụ 1: Lỗi khi dùng $ trong chuỗi thay thế
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacement {
public static void main(String[] args) {
String str = " <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>";
// Regex khớp nội dung thẻ bold
String regex = "<b>(\\S+)</b>";
System.out.println("Chuỗi đầu vào: \n" + str);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "sampledata$"); // $ gây lỗi
}
matcher.appendTail(sb);
System.out.println("Nội dung StringBuffer: \n" + sb.toString());
}
}
Kết quả (Lỗi)
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference: group index is missing
at java.util.regex.Matcher.appendReplacement(Unknown Source)
at QuoteReplacement.main(QuoteReplacement.java:18)
Phương thức quoteReplacement() của lớp Matcher chấp nhận một giá trị chuỗi và trả về một chuỗi thay thế chữ diện (literal replacement string). Tức là các ký tự / và $ bị bỏ qua trong chuỗi cho trước và kết quả có thể dùng làm tham số cho phương thức appendReplacement().
Ví dụ 2: Sử dụng quoteReplacement() để tránh lỗi
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacement {
public static void main(String[] args) {
String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>";
String regex = "<b>(\\S+)</b>";
System.out.println("Chuỗi đầu vào: \n" + str);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, Matcher.quoteReplacement("Bo$ld/Data$"));
}
matcher.appendTail(sb);
System.out.println("Nội dung StringBuffer: \n" + sb.toString());
}
}
Kết quả
Chuỗi đầu vào: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p> Nội dung StringBuffer: <p>This Bo$ld/Data$ an Bo$ld/Data$ HTML Bo$ld/Data$.</p>
Ví dụ 3: quoteReplacement() đơn giản
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteReplacementExample {
public static void main(String[] args) {
String input = "This is sample text";
String regex = "[#]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
String str = Matcher.quoteReplacement("sampledata");
System.out.println(str);
}
}
Kết quả
sampledata