- #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