Java不区分大小写的正则表达式匹配不符合字母Ñ

前端之家收集整理的这篇文章主要介绍了Java不区分大小写的正则表达式匹配不符合字母Ñ前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
考虑这个程序:
import java.util.regex.Pattern;
public class xx {

    /*
     *  Ñ
     *  LATIN CAPITAL LETTER N WITH TILDE
     *  Unicode: U+00D1,UTF-8: C3 91
     */
    public static final String BIG_N = "\u00d1";

    /*
     *  ñ
     *  LATIN SMALL LETTER N WITH TILDE
     *  Unicode: U+00F1,UTF-8: C3 B1
     */
    public static final String LITTLE_N = "\u00f1";

    public static void main(String[] args) throws Exception {
        System.out.println(BIG_N.equalsIgnoreCase(LITTLE_N));
        System.out.println(Pattern.compile(BIG_N,Pattern.CASE_INSENSITIVE).matcher(LITTLE_N).matches());
    }
}

由于Ñ是ñ的大写版本,您可以期望打印:

true
true

但它实际打印(java 1.7.0_17-b02)是:

true
false

为什么?

解决方法

By default,case-insensitive matching assumes that only characters in the US-ASCII charset are being matched. Unicode-aware case-insensitive matching can be enabled by specifying the UNICODE_CASE flag in conjunction with this flag.

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#CASE_INSENSITIVE

为了完整;你或(|)旗帜在一起.

Pattern.compile(BIG_N,Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)
原文链接:https://www.f2er.com/java/121409.html

猜你在找的Java相关文章