ซับนิพจน์/เมตาอักขระ “(?: re)” จัดกลุ่มนิพจน์ปกติโดยไม่จดจำข้อความที่จับคู่ได้ (non-capturing group)
ตัวอย่าง
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternExample {
public static void main(String args[]) {
// อ่านสตริงจากผู้ใช้
System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String input = sc.next();
String regex = "(?:[0-9])";
// คอมไพล์นิพจน์ปกติ
Pattern pattern = Pattern.compile(regex);
// ดึงออบเจ็กต์ matcher
Matcher matcher = pattern.matcher(input);
// ตรวจสอบการจับคู่
boolean bool = matcher.find();
if(bool) {
System.out.print("Match found");
} else {
System.out.print("Match not found");
}
}
}ผลลัพธ์
Enter a String 9848022338 Match found