java.util.regex.Matcher lớp đại diện cho một công cụ thực hiện các hoạt động so khớp khác nhau. Không có hàm tạo nào cho lớp này, bạn có thể tạo / lấy một đối tượng của lớp này bằng cách sử dụng phương thức match () của lớp java.util.regex.Pattern.
appendTail () phương thức của lớp này (Matcher) chấp nhận một đối tượng StringBuffer và nối các ký tự của chuỗi đầu vào vào nó.
Ví dụ
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AppendTail {
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>";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
matcher.appendTail(sb);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
System.out.println("Contents of the StringBuffer: \n"+ sb);
}
} Đầu ra
is example script Contents of the StringBuffer: <p>This <b>is</b> an <b>example</b> HTML <b>script</b>.</p>