Computer >> Máy Tính >  >> Lập trình >> Java

Phương thức Matcher quoteReplacement (String s) trong Java với các ví dụ

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ố và nối dữ liệu đầu vào vào đối tượng StringBuffer, thay thế nội dung phù hợ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à thêm bộ đệm Chuỗi, bất cứ khi nào khớp xảy ra, nó sẽ nối chuỗi thay thế thay vì phần nội dung phù hợp của chuỗi vào bộ đệm và tiến hành từ vị trí tiếp theo của chuỗi con đã khớp.

Trong khi chuyển chuỗi thay thế tới phương thức này, nếu bạn sử dụng “/” hoặc “$”, chúng sẽ không được coi là ký tự thông thường và xảy ra ngoại lệ -

Ví dụ 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() );
   }
}

Đầu ra

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)

Phương thức Thay thế trích dẫn của lớp Matcher chấp nhận một giá trị chuỗi và trả về một chuỗi thay thế theo nghĩa đen. tức là các ký tự / và $ bị bỏ qua trong chuỗi đã cho và kết quả có thể được sử dụng làm tham số cho appendReplacement () phương pháp.

Ví dụ 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() );
   }
}

Đầu ra

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>

Ví dụ 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);
   }
}

Đầu ra

sampledata