在 Objective-C 中匹配 CJK 扩展 B

Match CJK extension B in Objective-C

我在尝试匹配 NSString 中的 CJK 扩展 B 字符时遇到问题。

Wikipédia CJK Unified Ideographs Extension B :

CJK Unified Ideographs Extension B is a Unicode block containing rare and historic CJK ideographs for Chinese, Japanese, Korean, and Vietnamese.

字符的 unicode 块是:从 U+20000U+2A6DF 我正在使用正则表达式:[\ud840-\ud868][\udc00-\udfff]|\ud869[\udc00-\uded6]来匹配 CJK 扩展 B 字符。

这是我的代码:

NSString *searchedString = @""; // First character (U+20000) 

NSString *pattern = @"[\ud840-\ud868][\udc00-\udfff]|\ud869[\udc00-\uded6]";

 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
if ([regex numberOfMatchesInString:searchedString options:0 range:NSMakeRange(0, [searchedString length])] > 0) {
    NSLog(@"matches");
} else {
    NSLog(@"doesn't match");
}

输出:doesn't match

例如,如果我尝试对平假名字符进行更简单的操作,它会起作用:

NSString *searchedString = @"ひ";

NSString *pattern = @"[\u3040-\u309F]";

输出:matches

如有任何帮助,我们将不胜感激。谢谢。

您可以使用 \Uxxxxxxxx 符号来匹配 BMP 平面之外的那些 Unicode 字符。

符合。到 ICU regex docs:

\Uhhhhhhhh     Match the character with the hex value hhhhhhhh. Exactly eight hex digits must be provided, even though the largest Unicode code point is \U0010ffff.

所以,使用

NSString *pattern = @"[\U00020000-\U0002A6DF]+";

online Obj-C demo