使用NSRegularExpression检测字符串是否包含西里尔字母的示例 [英] Example of using NSRegularExpression to detect if string contains Cyrillic

查看:56
本文介绍了使用NSRegularExpression检测字符串是否包含西里尔字母的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 Apple参考NSRegularExpression上,并了解为了查看字符串是否为西里尔字母,我应该使用\p{script=cyrillic}.但是,在参考指南或

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

推荐答案

也许(我是iOS编程新手):

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

然后使用(NSString的类别可能更好?)

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

NSLog(@"hello: %hhd", [self containsCyrillic:@"hello"]);
NSLog(@"привет: %hhd", [self containsCyrillic:@"привет"]);

这篇关于使用NSRegularExpression检测字符串是否包含西里尔字母的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆