Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Java: ตรวจสอบ Multiple Regex Patterns ต่อ Input เดียว

มี 2 วิธีหลักในการตรวจสอบหลาย regex patterns ต่อ input เดียว:

วิธีที่ 1: ใช้ Alternation (|) ใน Regex เดียว

Metacharacter | แบ่ง patterns: exp1|exp2|exp3

ตัวอย่าง

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SampleExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your input: ");
        String input = sc.nextLine();
        
        // \d{10} = 10 digits (phone) | ^[aeiou] = เริ่มด้วยสระ
        String regex = "\\d{10}|^[aeiou]";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        
        if (matcher.find()) {
            System.out.println(input + " is valid");
        } else {
            System.out.println(input + " is not valid");
        }
    }
}

ผลลัพธ์

Enter your input: 9848033228
9848033228 is valid

Enter your input: an apple a day keeps doctor away
an apple a day keeps doctor away is valid

วิธีที่ 2: ใช้ List of Patterns

คอมไพล์แต่ละ regex เป็น Pattern แยกกัน เก็บใน List แล้ววนลูปตรวจสอบ

ตัวอย่าง

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MultipleRegex {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your input: ");
        String input = sc.nextLine();
        
        String regex1 = "\\d{10}";
        String regex2 = "^[aeiou]";
        
        Pattern pattern1 = Pattern.compile(regex1);
        Pattern pattern2 = Pattern.compile(regex2);
        
        List<Pattern> patterns = new ArrayList<>();
        patterns.add(pattern1);
        patterns.add(pattern2);
        
        for (Pattern pattern : patterns) {
            Matcher matcher = pattern.matcher(input);
            if (matcher.find()) {
                System.out.println("For regex " + pattern.pattern() + " " + input + " is valid");
            } else {
                System.out.println("For regex " + pattern.pattern() + " " + input + " is not valid");
            }
        }
    }
}

ผลลัพธ์

Enter your input: 9848033228
For regex \d{10} 9848033228 is valid
For regex ^[aeiou] 9848033228 is not valid

Enter your input: an apple a day keeps doctor away
For regex \d{10} an apple a day keeps doctor away is not valid
For regex ^[aeiou] an apple a day keeps doctor away is valid

เปรียบเทียบ

คุณสมบัติAlternation (|)List of Patterns
PerformanceCompile 1 ครั้ง, match 1 ครั้งCompile N ครั้ง, match N ครั้ง
ความยืดหยุ่นรวมเป็น 1 boolean (OR)รู้ว่า pattern ไหน match/ไม่ match
ComplexityRegex ใหญ่ อาจซับซ้อนแยก pattern ชัดเจน
Reusabilityยาก (ผสมใน regex เดียว)Pattern objects ใช้ซ้ำได้
Debuggingยาก (ไหน match ไหนไม่)ง่าย (เช็คแต่ละ pattern)

แนวทางแนะนำ

  • Patterns เรียบง่าย, ต้องการแค่ true/false → ใช้ |
  • Patterns ซับซ้อน, ต้องรู้ว่าไหน match, reuse patterns → ใช้ List
  • Production: Pre-compile patterns เป็น static final constants

Pre-compiled Patterns (Best Practice)


public class Validator {
    private static final Pattern PHONE = Pattern.compile("\\d{10}");
    private static final Pattern STARTS_WITH_VOWEL = Pattern.compile("^[aeiou]", Pattern.CASE_INSENSITIVE);
    private static final List<Pattern> PATTERNS = List.of(PHONE, STARTS_WITH_VOWEL);
    
    public static void validate(String input) {
        for (Pattern p : PATTERNS) {
            if (p.matcher(input).find()) {
                // valid for this pattern
            }
        }
    }
}