你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS App開辟中Objective-C應用正則表達式停止婚配的辦法

iOS App開辟中Objective-C應用正則表達式停止婚配的辦法

編輯:IOS開發綜合

IOS中有三種方法來完成正則表達式的婚配。如今將他們都記載在這裡:

1.應用NSPredicate(謂詞)婚配
例如婚配有用郵箱:

NSString *email = @“[email protected]”; 
NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; 
BOOL isValid = [predicate evaLuateWithObject:email];

謂詞婚配比擬靈巧,然則須要有謂詞的相干常識。

2.應用rangeOfString:option:直接查找

NSString *searchText = @"// Do any additional setup after loading the view, typically from a nib."; 
 
NSRange range = [searchText rangeOfString:@"(?:[^,])*\\." options:NSRegularExpressionSearch]; 
 
if (range.location != NSNotFound) { 
  NSLog(@"%@", [searchText substringWithRange:range]); 
}

options中設定NSRegularExpressionSearch就是表現應用正則表達式婚配,會前往第一個婚配成果的地位。

3.應用正則表達式類

NSString *searchText = @"// Do any additional setup after loading the view, typically from a nib.";  
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:[^,])*\\." options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:searchText options:0 range:NSMakeRange(0, [searchText length])];
if (result) {
  NSLog(@"%@\n", [searchText substringWithRange:result.range]);
}


應用體系的正則表達式類(NSRegularExpression)會前往婚配的多個成果。

實例:
1.驗證郵箱

+ (BOOL)validateEmail:(NSString *)email
{
  NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
  return [emailTest evaLuateWithObject:email];
}

NSPredicate是一個Foundation類,是用來查詢的,道理和用法都相似於SQL中的where。

2.驗證手機號

簡略的斷定辦法

+ (BOOL)validatePhone:(NSString *)phone
{
  NSString *phoneRegex = @"1[3|5|7|8|][0-9]{9}";
  NSPredicate *ph.netest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
  return [ph.netest evaLuateWithObject:phone];
}

這只是簡略地斷定手機號格局。其實手機的格局照樣有一點龐雜的。

具體的斷定辦法

//正則斷定手機號碼格局
+ (BOOL)validatePhone:(NSString *)phone
{
    /**
    * 手機號碼
    * 挪動: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}$";
    /**
         * 中國挪動:China Mobile
         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
         * 中國聯通:China Unicom
         * 130,131,132,152,155,156,185,186
         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
         * 中國電信:China Telecom
         * 133,1349,153,180,189
         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
         * 年夜海洋區固話及小閉塞
         * 區號:010,020,021,022,023,024,025,027,028,029
         * 號碼:七位或八位
         */
   // 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 *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
   NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

  if (([regextestmobile evaluateWithObject:phone] == YES)
  || ([regextestcm evaluateWithObject:phone] == YES)
  || ([regextestct evaluateWithObject:phone] == YES)
  || ([regextestcu evaluateWithObject:phone] == YES))
  {
    if([regextestcm evaluateWithObject:phone] == YES) {
     NSLog(@"China Mobile");
    } else if([regextestct evaluateWithObject:phone] == YES) {
     NSLog(@"China Telecom");
    } else if ([regextestcu evaluateWithObject:phone] == YES) {
     NSLog(@"China Unicom");
    } else {
     NSLog(@"Unknow");
    }

    return YES;
  }
  else 
  {
    return NO;
  }
}

【iOS App開辟中Objective-C應用正則表達式停止婚配的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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