码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 中文字符判断 正则NSRegularExpression 谓词NSPredicate 和 NSRange

时间:2017-08-29 23:47:14      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:pattern   chinese   orm   代码   sch   obj   regular   red   regex   

废话不说,直接上代码

#pragma mark - 中文字符判断

- (BOOL)validateContainsChinese:(NSString *)content
{
    NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
                                              initWithPattern:@"^[\u4e00-\u9fa5]"
                                              options:NSRegularExpressionCaseInsensitive
                                              error:nil];
    return ([regularexpression numberOfMatchesInString:content
                                               options:NSMatchingReportProgress
                                                 range:NSMakeRange(0, content.length)] > 0);
}

// 匹配是否为全中文
- (BOOL)validateAllChinese:(NSString *)content
{
    NSString *regEx = @"^[\u4e00-\u9fa5]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];
    
    return [predicate evaluateWithObject:content];
}

- (BOOL)validateContainsChineseT:(NSString *)content
{
    NSString *regEx = @"^[\u4e00-\u9fa5].+";  // @"^[\u4e00-\u9fa5].*"
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];
    
    return [predicate evaluateWithObject:content];
}

// 利用rangeOfString:option:直接查找
- (BOOL)validateRangeContainsChinese:(NSString *)content
{
    NSString *regEx = @"^[\u4e00-\u9fa5]";
    NSRange range = [content rangeOfString:regEx options:NSRegularExpressionSearch];
    
    return range.length;
    
}

// 利用rangeOfString:option:直接查找
- (BOOL)validateRangeAllChinese:(NSString *)content
{
    NSString *regEx = @"^[\u4e00-\u9fa5]+S";
    NSRange range = [content rangeOfString:regEx options:NSRegularExpressionSearch];
    
    return range.length;
    
}

 

iOS 中文字符判断 正则NSRegularExpression 谓词NSPredicate 和 NSRange

标签:pattern   chinese   orm   代码   sch   obj   regular   red   regex   

原文地址:http://www.cnblogs.com/pruple/p/7450654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!