Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Matcher quoteReplacement(String s) Method ใน Java พร้อมตัวอย่าง


เมธอด appendReplacement() ของคลาส Matcher รับ StringBuffer object และ String (replacement string) เป็นพารามิเตอร์ และนำข้อมูลอินพุตไปผนวกที่ StringBuffer object โดยแทนที่เนื้อหาที่จับคู่ได้ด้วย replacement string

ภายใน เมธอดนี้อ่านตัวอักษรแต่ละตัวจากสตริงอินพุตและผนวก String buffer ทุกครั้งที่มีการจับคู่ จะผนวก replacement string แทนส่วนของสตริงที่จับคู่ได้ และดำเนินการต่อจากตำแหน่งถัดไปของ substring ที่จับคู่ได้

ขณะส่ง replacement string ไปยังเมธอดนี้ หากคุณใช้ "/" หรือ "$" จะไม่ถูกพิจารณาว่าเป็น regular characters และจะเกิด exception ขึ้น −

ตัวอย่างที่ 1: ปัญหาเมื่อใช้ $ ใน replacement string

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);
        
        // สร้าง Pattern object
        Pattern pattern = Pattern.compile(regex);
        
        // จับคู่ pattern ที่คอมไพล์แล้วใน String
        Matcher matcher = pattern.matcher(str);
        
        // สร้าง StringBuffer ว่าง
        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());
    }
}

Output

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)

เมธอด quoteReplacement ของคลาส Matcher รับค่า string และคืนค่า literal replacement string กล่าวคือ ตัวอักษร / และ $ จะถูกละเว้นในสตริงที่ให้มา และผลลัพธ์สามารถใช้เป็นพารามิเตอร์สำหรับเมธอด appendReplacement() ได้

ตัวอย่างที่ 2: ใช้ quoteReplacement() แก้ปัญหา

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);
        
        // สร้าง Pattern object
        Pattern pattern = Pattern.compile(regex);
        
        // จับคู่ pattern ที่คอมไพล์แล้วใน String
        Matcher matcher = pattern.matcher(str);
        
        // สร้าง StringBuffer ว่าง
        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());
    }
}

Output

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: ใช้ quoteReplacement() แบบง่าย

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 object
        Pattern pattern = Pattern.compile(regex);
        
        // จับคู่ pattern ที่คอมไพล์แล้วใน String
        Matcher matcher = pattern.matcher(input);
        
        // สร้าง StringBuffer ว่าง
        String str = Matcher.quoteReplacement("sampledata");
        System.out.println(str);
    }
}

Output

sampledata