NSRegularExpression 正則表達式的 判斷和查找字符串

  
  
  
  
  1. #import "TestArithmetTwoAppDelegate.h" 
  2.  
  3.  
  4. @implementation TestArithmetTwoAppDelegate 
  5.  
  6. @synthesize window,testArray; 
  7.  
  8.  
  9. #pragma mark - 
  10. #pragma mark Application lifecycle 
  11.  
  12. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  13. {     
  14.     //測試數據數組 
  15.     self.testArray=[[NSMutableArray alloc ]  initWithObjects:@"dsfrf333",@"136158fs78910",@"2233333141808",@"13418141808",@"134",@"134135136138",nil];  
  16.      
  17.     //判斷是不是手機號碼 
  18.     [self phoneNumberJudge:testArray]; 
  19.      
  20.     //替換給定的字符 
  21.     [self replaceReguationItems:testArray]; 
  22.      
  23.     NSLog(@"==================大結局==========================="); 
  24.      
  25.     for (int i=0; i<testArray.count; i++)  
  26.     { 
  27.         NSLog(@"如今數組元素的值是==========%@",[testArray objectAtIndex:i]); 
  28.     } 
  29.      
  30.      
  31.     [self.window makeKeyAndVisible]; 
  32.     return YES; 
  33.  
  34.  
  35. -(void)phoneNumberJudge:(NSMutableArray*)aArray 
  36.   for (int i=0; i<aArray.count; i++)  
  37.   { 
  38.         NSMutableString *testString=[aArray objectAtIndex:i]; 
  39.        
  40.       //一個判斷是不是移動號碼的正則表達式 
  41.         NSRegularExpression *regularexpression = [[NSRegularExpression alloc] 
  42.                                                     initWithPattern:@"^(13[4-9]|15(8|9))[0-9]{8}$" 
  43.                                                     options:NSRegularExpressionCaseInsensitive 
  44.                                                     error:nil]; 
  45.          
  46.       //無符號整型數據接受匹配的數據的數目 
  47.         NSUInteger numberofMatch = [regularexpression numberOfMatchesInString:testString 
  48.                                                                         options:NSMatchingReportProgress 
  49.                                                                         range:NSMakeRange(0, testString.length)]; 
  50.                                      
  51.                                      
  52.         NSLog(@"================手機號碼測試%@=========",testString); 
  53.                                      
  54.         NSLog(@"11位移動手機號碼匹配的個數數%d",numberofMatch); 
  55.                                      
  56.         if(numberofMatch > 0) 
  57.             { 
  58.                NSLog(@"%@ is phone number: YES", testString); 
  59.             } 
  60.             else  
  61.             { 
  62.             NSLog(@"%@ is not phone number:", testString); 
  63.             } 
  64.    
  65.   } 
  66.      
  67.  
  68.  
  69.  
  70. -(void) replaceReguationItems:(NSMutableArray *) testarray 
  71.  
  72.     //正則表達式 
  73.      NSRegularExpression *regularexpression2 = [[NSRegularExpression alloc] 
  74.                                                initWithPattern:@"13[4-9]|15(8|9)" 
  75.                                                options:NSRegularExpressionCaseInsensitive 
  76.                                                error:nil]; 
  77.      
  78.      
  79.      
  80.      
  81.     for (int i=0; i<testarray.count; i++)  
  82.     { 
  83.       NSMutableString *testString=[testarray objectAtIndex:i]; 
  84.        
  85.        NSArray *matches = [regularexpression2  matchesInString:testString 
  86.                                                        options:0 
  87.                                                          range:NSMakeRange(0, [testString length])]; 
  88.      
  89.       NSLog(@"===========字符替換測試====當前字符串是%@=========",testString); 
  90.       
  91.       NSLog(@"%@中包含匹配項的個數是%d 個s",[testArray objectAtIndex:i],matches.count); 
  92.      
  93.        for (NSTextCheckingResult *match in matches) 
  94.       { 
  95.         NSMutableString *testString2=[testArray objectAtIndex:i]; 
  96.          
  97.         NSRange matchRange = [match range]; 
  98.         NSLog(@"當前的location==%d,當前的的length==%d",matchRange.location,matchRange.length); 
  99.  
  100.         //替換字符串 
  101.         NSString  *template=@"我被替換了"; 
  102.          
  103.            
  104.         NSString *newstring=[regularexpression2 stringByReplacingMatchesInString:testString2 
  105.                                                                          options:0 
  106.                                                                            range:matchRange 
  107.                                                                     withTemplate:template];   
  108.          
  109.         [testArray  replaceObjectAtIndex:i withObject:newstring]; 
  110.          
  111.         NSLog(@"%@ is changed:", [testArray objectAtIndex:i]); 
  112.      } 
  113.      
  114.  
  115.     } 
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. - (void)dealloc { 
  124.     [testArray release]; 
  125.     [window release]; 
  126.     [super dealloc]; 
  127.  
  128.  
  129. @end 
相關文章
相關標籤/搜索