Java case insensitive regular expression matching does not match the letter Ñ
•
Java
Consider this procedure:
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()); } }
Since Ñ is an uppercase version of ñ, you can expect to print:
true true
But the actual print (Java 1.7.0_17-b02) is:
true false
Why?
Solution
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#CASE_INSENSITIVE
For completeness; You or (|) flags together
Pattern.compile(BIG_N,Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码