Java 中的模式 UNICODE_CASE 字段示例

javaobject oriented programmingprogramming更新于 2024/5/8 9:16:17

启用 Unicode 感知大小写折叠。

当您将此作为标志值与 CASE_INSENSITIVE 标志一起用于 compile() 方法时,如果您使用正则表达式搜索 Unicode 字符,则两种大小写的 Unicode 字符都会匹配。

示例

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UNICODE_CASE_Example {
   public static void main( String args[] ) {
      String regex = "\u00de";
      //编译正则表达式
      Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE|Pattern.CASE_INSENSITIVE);
      //检索匹配器对象
      String str[] = {"\u00de", "\u00fe", "\u00ee", "\u00ce"};
      for (String ele : str) {
         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 Þ
þ is a match for Þ
î is not a match for Þ
Î is not a match for Þ

相关文章


有用资源