iOS正则表达式判断包含或全部为中文、字母、字数

//字符串全为中文组成

- (BOOL)stringShouldChinese:(NSString*)string{

    NSString *regex = @"[\u4e00-\u9fa5]+";

    NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    if(![predevaluateWithObject:string]){

        returnNO;

    }

    return YES;

}

//字符串全为数字组成

- (BOOL)stringShouldNumber:(NSString*)string{

    NSString*regex =@"[0-9]*";

    NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    if(![predevaluateWithObject:string]){

        returnNO;

    }

    return YES;

}

//字符串全为字母组成

- (BOOL)stringShouldABC:(NSString*)string{

    NSString*regex =@"[a-zA-Z]*";

    NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    if(![predevaluateWithObject:string]){

        returnNO;

    }

    return YES;

}

//字符串为字母、数字组成

- (BOOL)stringShouldNumberAndABC:(NSString*)string{

    NSString *regex =@"[a-zA-Z0-9]*";

    NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    if(![predevaluateWithObject:string]) {

        returnNO;

    }

    return YES;


}

//字符串为中文、字母、数字组成

- (BOOL)stringShouldAlphaNumChinese:(NSString*)string{

    NSString *regex =@"[a-zA-Z0-9\u4e00-\u9fa5]*";

    NSPredicate*pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex];

    if(![predevaluateWithObject:string]){

        returnNO;

    }

    return YES;

}

//判断是否有中文

-(BOOL)stringContainChinese:(NSString*)str {

    for(inti=0; i< [strlength];i++){

        inta = [strcharacterAtIndex:i];

        if( a >0x4e00&& a <0x9fff)

        {

            returnYES;

        }

    }

    return NO;

}

//判断是否有数字

- (BOOL)stringContainNumber:(NSString*)str {

    NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];//count是str中包含[0-9]数字的个数,只要count>0,说明str中包含数字

    if(count >0){

        returnYES;

    }

    return NO;

}

//判断是否有字母

- (BOOL)stringContainABC:(NSString*)str {

    NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];//count是str中包含[A-Za-z]数字的个数,只要count>0,说明str中包含数字

    if(count >0) {

        returnYES;

    }

    return NO;

}

//判断字符串中包含字母和数字

- (BOOL)isStringContainABCandNumberWith:(NSString*)str {

    NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z0-9]" options:NSRegularExpressionCaseInsensitive error:nil];

    NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];//count是str中包含[A-Za-z0-9]数字的个数,只要count>0,说明str中包含数字

    if(count >0) {

        returnYES;

    }

    return NO;


}

你可能感兴趣的:(iOS正则表达式判断包含或全部为中文、字母、字数)