不区分大小写 POSIX 正则表达式在 Java Pattern & Matcher 中不区分大小写
Case-insensitive POSIX regex is not case-insensitive in Java Pattern & Matcher
我不是 Regex 方面的专家,这可能是一个显而易见的原因,但我找不到这个问题的答案。
我使用 POSIX 表示法以不区分大小写的方式在 Java 中使用正则表达式来匹配字符串 (n
)。给定:
Pattern pattern = Pattern.compile("\p{Upper}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("n");
为什么以下代码会导致 false
?
boolean find = matcher.find();
在 Pattern
文档中,我发现了以下内容(强调我的):
\p{Upper} An upper-case alphabetic character: [A-Z]
针对正则表达式 [A-Z]
进行测试,true
中的结果如下:
Pattern pattern = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("n");
boolean find = matcher.find();
有什么区别?
无论是对还是错 - Posix 字符 class 会忽略 CASE_INSENSITIVE
标志。尽管 \p{Upper}
与 [A-Z]
的工作方式类似,但并不完全相同 - 而且它不考虑不区分大小写的标志。
Pattern
class 中检查位置字符 classes 的代码没有引用 CASE_INSENSITIVE
标志:
/**
* 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);
}
}
来自 POSIX 规范 (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.
当使用POSIX字符类时,Pattern.CASE_INSENSITIVE
不会忽略大小写对应检查。
我不是 Regex 方面的专家,这可能是一个显而易见的原因,但我找不到这个问题的答案。
我使用 POSIX 表示法以不区分大小写的方式在 Java 中使用正则表达式来匹配字符串 (n
)。给定:
Pattern pattern = Pattern.compile("\p{Upper}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("n");
为什么以下代码会导致 false
?
boolean find = matcher.find();
在 Pattern
文档中,我发现了以下内容(强调我的):
\p{Upper} An upper-case alphabetic character: [A-Z]
针对正则表达式 [A-Z]
进行测试,true
中的结果如下:
Pattern pattern = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("n");
boolean find = matcher.find();
有什么区别?
无论是对还是错 - Posix 字符 class 会忽略 CASE_INSENSITIVE
标志。尽管 \p{Upper}
与 [A-Z]
的工作方式类似,但并不完全相同 - 而且它不考虑不区分大小写的标志。
Pattern
class 中检查位置字符 classes 的代码没有引用 CASE_INSENSITIVE
标志:
/**
* 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);
}
}
来自 POSIX 规范 (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.
当使用POSIX字符类时,Pattern.CASE_INSENSITIVE
不会忽略大小写对应检查。