你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iOS中正則表達式的使用–NSPredicate

iOS中正則表達式的使用–NSPredicate

編輯:關於IOS

首先舉一個例子:
匹配9-15個由字母/數字組成的字符串的正則表達式:
    NSString * regex = @"^[A-Za-z0-9]{9,15}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:txtfldPhoneNumber.text];

Cocoa用NSPredicate描述查詢的方式,原理類似於在數據庫中進行查詢

用BETWEEN,IN,BEGINWITH,ENDWITH,CONTAINS,LIKE這些謂詞來構造NSPredicate,必要的時候使用SELF直接對自己進行匹配

 

[cpp]
//基本的查詢  
NSPredicate *predicate; 
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"]; 
    BOOL match = [predicate evaluateWithObject: car]; 
    NSLog (@"%s", (match) ? "YES" : "NO"); 
//在整個cars裡面循環比較  
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"]; 
    NSArray *cars = [garage cars]; 
    for (Car *car in [garage cars]) { 
        if ([predicate evaluateWithObject: car]) { 
            NSLog (@"%@", car.name); 
        } 
    } 
//輸出完整的信息  
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"]; 
    NSArray *results; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
//含有變量的謂詞  
    NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"]; 
    NSDictionary *varDict; 
    varDict = [NSDictionary dictionaryWithObjectsAndKeys: 
               @"Herbie", @"NAME", nil]; 
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict]; 
    NSLog(@"SNORGLE: %@", predicate); 
    match = [predicate evaluateWithObject: car]; 
  NSLog (@"%s", (match) ? "YES" : "NO"); 
//注意不能使用$VARIABLE作為路徑名,因為它值代表值  
//謂詞字符竄還支持c語言中一些常用的運算符  
    
    predicate = [NSPredicate predicateWithFormat: 
                 @"(engine.horsepower > 50) AND (engine.horsepower < 200)"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"oop %@", results); 
    
    predicate = [NSPredicate predicateWithFormat: @"name < 'Newton'"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", [results valueForKey: @"name"]); 
//強大的數組運算符  
    predicate = [NSPredicate predicateWithFormat: 
                 @"engine.horsepower BETWEEN { 50, 200 }"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
    
    NSArray *betweens = [NSArray arrayWithObjects: 
                         [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil]; 
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
    predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"]; 
    varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil]; 
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
//IN運算符  
    predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", [results valueForKey: @"name"]); 
    predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", [results valueForKey: @"name"]); 
    
    names = [cars valueForKey: @"name"]; 
    predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; 
    results = [names filteredArrayUsingPredicate: predicate];//這裡限制了SELF的范圍  
    NSLog (@"%@", results); 
//BEGINSWITH,ENDSWITH,CONTAINS  
//附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字符,cd表示什麼都不區分  
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
    
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
    
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
//LIKE運算符(通配符)  
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 
    
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"]; 
    results = [cars filteredArrayUsingPredicate: predicate]; 
    NSLog (@"%@", results); 

//基本的查詢
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];
    BOOL match = [predicate evaluateWithObject: car];
    NSLog (@"%s", (match) ? "YES" : "NO");
//在整個cars裡面循環比較
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
    NSArray *cars = [garage cars];
    for (Car *car in [garage cars]) {
        if ([predicate evaluateWithObject: car]) {
            NSLog (@"%@", car.name);
        }
    }
//輸出完整的信息
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
    NSArray *results;
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
//含有變量的謂詞
    NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
    NSDictionary *varDict;
    varDict = [NSDictionary dictionaryWithObjectsAndKeys:
               @"Herbie", @"NAME", nil];
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
    NSLog(@"SNORGLE: %@", predicate);
    match = [predicate evaluateWithObject: car];
  NSLog (@"%s", (match) ? "YES" : "NO");
//注意不能使用$VARIABLE作為路徑名,因為它值代表值
//謂詞字符竄還支持c語言中一些常用的運算符
  
    predicate = [NSPredicate predicateWithFormat:
                 @"(engine.horsepower > 50) AND (engine.horsepower < 200)"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"oop %@", results);
  
    predicate = [NSPredicate predicateWithFormat: @"name < 'Newton'"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", [results valueForKey: @"name"]);
//強大的數組運算符
    predicate = [NSPredicate predicateWithFormat:
                 @"engine.horsepower BETWEEN { 50, 200 }"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
  
    NSArray *betweens = [NSArray arrayWithObjects:
                         [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
    predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];
    varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
//IN運算符
    predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", [results valueForKey: @"name"]);
    predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", [results valueForKey: @"name"]);
  
    names = [cars valueForKey: @"name"];
    predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
    results = [names filteredArrayUsingPredicate: predicate];//這裡限制了SELF的范圍
    NSLog (@"%@", results);
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符號,[c],[d],[cd],c表示不區分大小寫,d表示不區分發音字符,cd表示什麼都不區分
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
  
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
  
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
//LIKE運算符(通配符)
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);
  
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];
    results = [cars filteredArrayUsingPredicate: predicate];
    NSLog (@"%@", results);

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved