문제

I looked into the Apple reference on NSRegularExpression, and understand that in order see if the string is Cyrillic I should use \p{script=cyrillic}. However, I have not been able to find an actual example of how this is done in the reference guide or in an answer located in SO. What I would ideally like to achieve is:

if (string contains \p{script=cyrillic}){
    return YES;
    }
else {
    return NO;
    }
도움이 되었습니까?

해결책

Maybe (I am an iOS programming newbie):

- (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;
}

And then the usage (a category for NSString would probably be better?)

NSLog(@"hello: %hhd", [self containsCyrillic:@"hello"]);
NSLog(@"привет: %hhd", [self containsCyrillic:@"привет"]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top