วิธีการ appendReplacement() ของคลาส Matcher ยอมรับวัตถุ StringBuffer และ String (สตริงการแทนที่) เป็นพารามิเตอร์ และผนวกข้อมูลที่ป้อนเข้าไปยังวัตถุ StringBuffer โดยแทนที่เนื้อหาที่ตรงกันด้วยสตริงการแทนที่
ภายในเมธอดนี้จะอ่านอักขระแต่ละตัวจากสตริงอินพุตและต่อท้ายบัฟเฟอร์สตริง เมื่อใดก็ตามที่มีการจับคู่เกิดขึ้น จะผนวกสตริงการแทนที่แทนส่วนเนื้อหาที่ตรงกันของสตริงไปยังบัฟเฟอร์ และดำเนินการจากตำแหน่งถัดไปของสตริงย่อยที่ตรงกัน
ขณะส่งสตริงการแทนที่ไปยังวิธีนี้หากคุณใช้ “/” หรือ “$” จะไม่ถือว่าเป็นอักขระปกติและมีข้อยกเว้นเกิดขึ้น -
ตัวอย่างที่ 1
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>";
//Regular expression to match contents of the bold tags
String regex = "<b>(\\S+)</b>";
System.out.println("Input string: \n"+str);
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "sampledata$" );
//Matcher.quoteReplacement("Bo$ld/Data$"));
}
matcher.appendTail(sb);
System.out.println("Contents of the StringBuffer: \n"+ sb.toString() );
}
} ผลลัพธ์
Input string:<p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference: group index is missing at java.util.regex.Matcher.appendReplacement(Unknown Source) at OCTOBER.matcher.QuoteReplacement.main(QuoteReplacement.java:18)
วิธีเปลี่ยนเครื่องหมายคำพูดของคลาส Matcher ยอมรับค่าสตริงและส่งกลับสตริงการแทนที่ตามตัวอักษร กล่าวคือ อักขระ / และ $ จะถูกละเว้นในสตริงที่กำหนด และผลลัพธ์สามารถใช้เป็นพารามิเตอร์สำหรับ appendReplacement() วิธีการ
ตัวอย่างที่ 2
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>";
//Regular expression to match contents of the bold tags
String regex = "<b>(\\S+)</b>";
System.out.println("Input string: \n"+str);
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, Matcher.quoteReplacement("Bo$ld/Data$"));
}
matcher.appendTail(sb);
System.out.println("Contents of the StringBuffer: \n"+ sb.toString() );
}
} ผลลัพธ์
Input string: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p> Contents of the StringBuffer: <p>This Bo$ld/Data$ an Bo$ld/Data$ HTML Bo$ld/Data$.</p>
ตัวอย่างที่ 3
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 = "[#]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
//Creating an empty string buffer
String str = Matcher.quoteReplacement("sampledata");
System.out.println(str);
}
} ผลลัพธ์
sampledata