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

Pattern CANON_EQ Field ใน Java พร้อมตัวอย่าง


Field CANON_EQ ของคลาส Pattern จับคู่ตัวอักษรสองตัวก็ต่อเมื่อพวกมัน canonically equal เท่านั้น เมื่อคุณใช้เป็น flag value ในเมธอด compile() ตัวอักษรสองตัวจะถูกจับคู่ก็ต่อเมื่อ full canonical decompositions ของพวกมันเท่ากัน

ที่ canonical decomposition เป็นหนึ่งในรูปแบบ Unicode text normalization

ตัวอย่างที่ 1: match b\u0307 กับ \u1E03

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

public class CANON_EQ_Example {
    public static void main(String args[]) {
        String regex = "b\u0307";
        
        // คอมไพล์ regular expression
        Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ);
        
        // ดึง Matcher object
        Matcher matcher = pattern.matcher("\u1E03");
        if (matcher.matches()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
    }
}

Output

Match found

ตัวอย่างที่ 2: match a\u030A กับตัวเลือกต่างๆ

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

public class CANON_EQ_Example {
    public static void main(String args[]) {
        String regex = "a\u030A";
        
        // คอมไพล์ regular expression
        Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ);
        
        // ดึง Matcher object
        String [] input = {"\u00E5", "a\u0311", "a\u0325", "a\u030A", "a\u1E03", "a\uFB03"};
        for (String ele : input) {
            Matcher matcher = pattern.matcher(ele);
            if (matcher.matches()) {
                System.out.println(ele + " is a match for " + regex);
            } else {
                System.out.println(ele + " is not a match for " + regex);
            }
        }
    }
}

Output

å is a match for a?
a? is not a match for a?
a? is not a match for a?
a? is a match for a?
a? is not a match for a?
a? is not a match for a?