我查看了Apple reference在 NSRegularExpression
,并了解为了查看字符串是否是西里尔字母,我应该使用 \p{script=cyrillic}
.但是,我无法在引用指南或 answer 中找到如何完成此操作的实际示例。位于SO。我理想地想要实现的是:
if (string contains \p{script=cyrillic}){
return YES;
}
else {
return NO;
}
最佳答案
也许(我是 iOS 编程新手):
- (BOOL)containsCyrillic:(NSString*)str
{
NSString* const pattern = @"\\p{script=cyrillic}+";
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:pattern
options:0
error:nil];
NSRange range = NSMakeRange(0, [str length]);
return [regex numberOfMatchesInString:str
options:0
range:range] > 0;
}
然后是用法(
NSString
的类别可能会更好?)NSLog(@"hello: %hhd", [self containsCyrillic:@"hello"]);
NSLog(@"привет: %hhd", [self containsCyrillic:@"привет"]);
关于ios - 使用 NSRegularExpression 检测字符串是否包含西里尔字母的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275994/