Phần biểu thức/metaký tự “\Z” khớp với cuối toàn bộ chuỗi ngoại trừ ký tự kết thúc dòng cho phép (allowable final line terminator).
Ví dụ 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String args[]) {
String regex = "Tutorialspoint\\z";
String input = "Hi how are you welcome to Tutorialspoint";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("Số lần khớp: " + count);
}
}
Kết quả
Số lần khớp: 1
Ví dụ 2
Chương trình Java sau kiểm tra xem văn bản đầu vào có kết thúc bằng chữ số không.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Data {
public static void main(String args[]) {
String regex = "[0-9]\\z";
String input = "Hi how are you \n this is sample text \n this is third line 554";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println("Đầu vào đã cho kết thúc bằng chữ số");
} else {
System.out.println("Đầu vào đã cho không kết thúc bằng chữ số");
}
}
}
Kết quả
Đầu vào đã cho kết thúc bằng chữ số