Java 中的模式 CANON_EQ 字段示例

javaobject oriented programmingprogramming更新于 2024/5/7 5:00:17

Pattern 类的 CANON_EQ 字段仅在两个字符规范相等时才匹配它们。当您将其用作 compile() 方法的标志值时,当且仅当两个字符的完整规范分解相等时,才会匹配它们。

其中规范分解是 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";
      //编译正则表达式
      Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ);
      //检索匹配器对象
      Matcher matcher = pattern.matcher("\u1E03");
      if(matcher.matches()) {
         System.out.println("找到匹配项");
      } else {
           System.out.println("未找到匹配项");
      }
   }
}

输出

找到匹配项

示例 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";
      //编译正则表达式
      Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ);
      //检索匹配器对象
      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?

相关文章


有用资源