คุณสามารถจับคู่ปลายทางของอินพุตได้โดยใช้เมตาอักขระ “\\z”
ตัวอย่าง
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String args[]) {
// อ่านสตริงจากผู้ใช้
System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String regex = "[0-9]\\z";
// คอมไพล์นิพจน์ปกติ
Pattern pattern = Pattern.compile(regex);
// ดึงออบเจ็กต์ matcher
Matcher matcher = pattern.matcher(input);
int count = 0;
if(matcher.find()) {
System.out.println("Match found ");
} else {
System.out.println("Match not found ");
}
}
}ผลลัพธ์ที่ 1
Enter a String sample text Match not found
ผลลัพธ์ที่ 2
Enter a String sample text 23 Match found