- #import "TestArithmetTwoAppDelegate.h"
-
-
- @implementation TestArithmetTwoAppDelegate
-
- @synthesize window,testArray;
-
-
- #pragma mark -
- #pragma mark Application lifecycle
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- //測試數據數組
- self.testArray=[[NSMutableArray alloc ] initWithObjects:@"dsfrf333",@"136158fs78910",@"2233333141808",@"13418141808",@"134",@"134135136138",nil];
-
- //判斷是不是手機號碼
- [self phoneNumberJudge:testArray];
-
- //替換給定的字符
- [self replaceReguationItems:testArray];
-
- NSLog(@"==================大結局===========================");
-
- for (int i=0; i<testArray.count; i++)
- {
- NSLog(@"如今數組元素的值是==========%@",[testArray objectAtIndex:i]);
- }
-
-
- [self.window makeKeyAndVisible];
- return YES;
- }
-
-
- -(void)phoneNumberJudge:(NSMutableArray*)aArray
- {
- for (int i=0; i<aArray.count; i++)
- {
- NSMutableString *testString=[aArray objectAtIndex:i];
-
- //一個判斷是不是移動號碼的正則表達式
- NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
- initWithPattern:@"^(13[4-9]|15(8|9))[0-9]{8}$"
- options:NSRegularExpressionCaseInsensitive
- error:nil];
-
- //無符號整型數據接受匹配的數據的數目
- NSUInteger numberofMatch = [regularexpression numberOfMatchesInString:testString
- options:NSMatchingReportProgress
- range:NSMakeRange(0, testString.length)];
-
-
- NSLog(@"================手機號碼測試%@=========",testString);
-
- NSLog(@"11位移動手機號碼匹配的個數數%d",numberofMatch);
-
- if(numberofMatch > 0)
- {
- NSLog(@"%@ is phone number: YES", testString);
- }
- else
- {
- NSLog(@"%@ is not phone number:", testString);
- }
-
- }
-
- }
-
-
-
- -(void) replaceReguationItems:(NSMutableArray *) testarray
- {
-
- //正則表達式
- NSRegularExpression *regularexpression2 = [[NSRegularExpression alloc]
- initWithPattern:@"13[4-9]|15(8|9)"
- options:NSRegularExpressionCaseInsensitive
- error:nil];
-
-
-
-
- for (int i=0; i<testarray.count; i++)
- {
- NSMutableString *testString=[testarray objectAtIndex:i];
-
- NSArray *matches = [regularexpression2 matchesInString:testString
- options:0
- range:NSMakeRange(0, [testString length])];
-
- NSLog(@"===========字符替換測試====當前字符串是%@=========",testString);
-
- NSLog(@"%@中包含匹配項的個數是%d 個s",[testArray objectAtIndex:i],matches.count);
-
- for (NSTextCheckingResult *match in matches)
- {
- NSMutableString *testString2=[testArray objectAtIndex:i];
-
- NSRange matchRange = [match range];
- NSLog(@"當前的location==%d,當前的的length==%d",matchRange.location,matchRange.length);
-
- //替換字符串
- NSString *template=@"我被替換了";
-
-
- NSString *newstring=[regularexpression2 stringByReplacingMatchesInString:testString2
- options:0
- range:matchRange
- withTemplate:template];
-
- [testArray replaceObjectAtIndex:i withObject:newstring];
-
- NSLog(@"%@ is changed:", [testArray objectAtIndex:i]);
- }
-
-
- }
-
-
- }
-
-
-
-
-
- - (void)dealloc {
- [testArray release];
- [window release];
- [super dealloc];
- }
-
-
- @end