PredicateAndRegularExpression

来源:互联网 发布:ubuntu etc目录在哪 编辑:程序博客网 时间:2024/06/10 11:27

他人关于正则表达式的总结点击打开链接

<span style="font-size:18px;">////  ViewController.m//  Predicate////  Created by LWJ on 15-10-31.//  Copyright (c) 2015年 laodao. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSArray *array = @[@"beijing",                       @"shanghai",                       @"shenzheng",                       @"guangzhou",                       @"zhengzhou",                       @"ZHNEGZHOU"];    /**     *  谓词     */    /***  判断是否包含某个字符*******************/    NSPredicate *preOne = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'an'"];    /**     *  filter 过滤器     */    NSArray *preOneArray = [array filteredArrayUsingPredicate:preOne];    NSLog(@"1结果是:::::%@",preOneArray);//    NSLog(@"%@",[NSString stringWithFormat:@"======d%d'45'"]);    /***************以某个字符串开头*************/        NSPredicate *preTwo = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH [c] 'zh'"];    NSArray *preTwoArray = [array filteredArrayUsingPredicate:preTwo];    NSLog(@"2结果是:::::%@",preTwoArray);    /***************以某个字符串结尾******************/    NSPredicate *predicateThree = [NSPredicate predicateWithFormat:@"SELF ENDSWITH [c] 'ou'"];    NSArray *preThreeArr = [array filteredArrayUsingPredicate:predicateThree];    NSLog(@"3的结果是:::%@",preThreeArr);    /***********单独查询是否包含某个字符串***********/    /**     *  字符串查询不能支持模糊查询     */    NSString *string = @"beijing";    NSPredicate *preStr = [NSPredicate predicateWithFormat:@"SELF == %@",string];    NSArray *preStrArr = [array filteredArrayUsingPredicate:preStr];    NSLog(@"4的结果是::%@",preStrArr);    /******************like模糊查询*****************/    NSPredicate *preLikeOne = [NSPredicate predicateWithFormat:@"SELF like [c] %@",@"*ng*"];    //* 代表任意个数字符    NSArray *preLikeOneArr = [array filteredArrayUsingPredicate:preLikeOne];    NSLog(@"5------%@",preLikeOneArr);    //? 代表任意字符 而且只能代表一个位置    NSPredicate *preLikeTwo = [NSPredicate predicateWithFormat:@"SELF like '???ng*'"];    NSArray *preLikeTwoArr = [array filteredArrayUsingPredicate:preLikeTwo];    NSLog(@"6------%@",preLikeTwoArr);    /************比较运算符****************/    NSNumber *number = @12;    NSPredicate *preNum = [NSPredicate predicateWithFormat:@"SELF<20"];    /**     *  evaluation 评价 估值     */    BOOL isBiger = [preNum evaluateWithObject:number];    NSLog(@"7======%@",isBiger?@"YES":@"NO");    /*************正则表达式*****/    NSString *mailStr = @"asdasdasda@asad.com";    /**     *  判断是否是一个标准的邮箱格式   并不能判断是否有效     */    BOOL isMail = [self isValidateEmail:mailStr];    NSLog(@"8 mail is true ==== %@", isMail?@"YES":@"NO");        NSString *phoneStr = @"13271719960";        BOOL isPhone = [self isMobileNumber:phoneStr];    NSLog(@"is Phone===  ==%@",isPhone?@"yes":@"no");}//验证email- (BOOL)isValidateEmail:(NSString *)email{    NSString *strRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}";//    @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}"    BOOL result = [self isValidateRegularExpression:email byExpression:strRegex];    return result;}//是否有效的正则表达式- (BOOL)isValidateRegularExpression:(NSString *)strDestination byExpression:(NSString *)strExpression{    /**     *  matches 相配 比较     */    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",strExpression];    return [predicate evaluateWithObject:strDestination];}//验证11位号码- (BOOL) isValildateTelNumber:(NSString *)number{    NSString *strRegex = @"[0-9]{11,11}";    BOOL result = [self isValidateRegularExpression:number byExpression:strRegex];    return  result;}//正则判断手机号码地址格式- (BOOL)isMobileNumber:(NSString *)mobileNum{    /**     * 手机号码     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188     * 联通:130,131,132,152,155,156,185,186     * 电信:133,1349,153,180,189     */    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";    /**     10         * 中国移动:China Mobile     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188     12         */    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";    /**     15         * 中国联通:China Unicom     16         * 130,131,132,152,155,156,185,186     17         */    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";    /**     20         * 中国电信:China Telecom     21         * 133,1349,153,180,189     22         */    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";    /**     25         * 大陆地区固话及小灵通     26         * 区号:010,020,021,022,023,024,025,027,028,029     27         * 号码:七位或八位     28         */    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";        NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];    if ([regextestmobile evaluateWithObject:mobileNum]        ||[regextestcm evaluateWithObject:mobileNum]        ||[regextestct evaluateWithObject:mobileNum]        ||[regextestcu evaluateWithObject:mobileNum])    {        if ([regextestcm evaluateWithObject:mobileNum])        {            NSLog(@"China Mobile");        }        else if([regextestct evaluateWithObject:mobileNum])        {            NSLog(@"China Telecom");        }        else if([regextestcu evaluateWithObject:mobileNum])        {            NSLog(@"China Unicom");        }        else        {            NSLog(@"UnKnow");        }        return YES;    }    else    {        return NO;    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end</span>


0 0
原创粉丝点击