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

รูปแบบฟิลด์ CANON_EQ ใน Java พร้อมตัวอย่าง


ฟิลด์ CANON_EQ ของคลาส Pattern จะจับคู่อักขระสองตัวก็ต่อเมื่อมีค่าเท่ากันตามบัญญัติ เมื่อคุณใช้ค่านี้เป็นค่าแฟล็กสำหรับวิธีการคอมไพล์ () อักขระสองตัวจะถูกจับคู่ก็ต่อเมื่อการสลายตัวตามรูปแบบบัญญัติทั้งหมดมีค่าเท่ากัน

โดยที่การสลายตัวตามรูปแบบบัญญัติเป็นหนึ่งในรูปแบบการทำให้เป็นมาตรฐานของข้อความ Unicode

ตัวอย่างที่ 1

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";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ);
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher("\u1E03");
      if(matcher.matches()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
   }
}

ผลลัพธ์

Match found

ตัวอย่างที่ 2

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";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ);
      //Retrieving the 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);
         }
      }
   }
}

ผลลัพธ์

å 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?