简体   繁体   中英

Case-insensitive POSIX regex is not case-insensitive in Java Pattern & Matcher

I am not an expert in Regex, might be an obvious reason, but I cannot find an answer to this.

I use a POSIX notation to match a String ( n ) using Regex in Java in a case-insensitive way. Given:

Pattern pattern = Pattern.compile("\\p{Upper}", Pattern.CASE_INSENSITIVE); 
Matcher matcher = pattern.matcher("n");

Why the following code results in false ?

boolean find = matcher.find();

In the Pattern documentation, I found the following (emphesizes mine):

\\p{Upper} An upper-case alphabetic character: [AZ]

Tested against the Regex [AZ] , the following results in true :

Pattern pattern = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE); 
Matcher matcher = pattern.matcher("n");
boolean find = matcher.find();

What is the difference?

Whether for right or for wrong - the Posix character classes ignore the CASE_INSENSITIVE flag. Although \\p{Upper} works like [AZ] , it's not exactly the same - and it doesn't look at the case insensitive flag.

The code in the Pattern class that checks posic character classes doesn't refer to the CASE_INSENSITIVE flag:

/**
 * Node class that matches a POSIX type.
 */
static final class Ctype extends BmpCharProperty {
    final int ctype;
    Ctype(int ctype) { this.ctype = ctype; }
    boolean isSatisfiedBy(int ch) {
        return ch < 128 && ASCII.isType(ch, ctype);
    }
}

From POSIX specification (IEEE 1003):

9.2 Regular Expression General Requirements

When a standard utility or function that uses regular expressions specifies that pattern matching shall be performed without regard to the case (uppercase or lowercase) of either data or patterns, then when each character in the string is matched against the pattern, not only the character, but also its case counterpart (if any), shall be matched.

When using POSIX character classes, Pattern.CASE_INSENSITIVE doesn't make it ignore the case counterpart check.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM