正则表达式过滤手机号

最近写了手机卫士,有个小需求是将系统任何地方复制得到的手机号码 粘贴到自己的手机卫士App中查询该号码信息,那么就有个去特殊符号的问题,只留数字,另外需要自动粘贴到自己App输入框,所以呢在 下面的方法 获取粘贴板内容并处理号码为纯数字结果:(PS :最近流行的微信复制淘宝链接内容,打开淘宝即可跳转到相应的 页面,使用的也是此技术要点,当下搬运工,参考:http://www.jianshu.com/p/10a6900cc904)

- (void)applicationDidBecomeActive:(UIApplication *)application {
    UIPasteboard *paste = [UIPasteboard generalPasteboard];
    if (paste.string.length>=7) {
        
        NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:0 error:NULL];
        //方法一去掉不符合规则的字符  @"a-zA-Z.-*#";
//        NSString *resultString = [regular stringByReplacingMatchesInString:paste.string options:0 range:NSMakeRange(0, paste.string.length) withTemplate:@""];
//        NSLog(@"pasteBoardString = %@ result = %@",paste.string,resultString);
        
        NSArray *arr =[regular matchesInString:paste.string options:0 range:NSMakeRange(0, paste.string.length)];
        NSString *string = @"";
        for (NSTextCheckingResult *res in arr) {
            string = [string stringByAppendingFormat:@"%@",[paste.string substringWithRange:res.range]];
        }
        NSLog(@"phone number = %@",string);
     
        if (string.length>=7) {
            BaseNavigationController * nav =  (BaseNavigationController *)self.window.rootViewController;
            
            if ([nav.topViewController isKindOfClass:[ViewController class]]) {
                ViewController *rootVC = (ViewController *)nav.topViewController;
                rootVC.pastenNmber = string;
                paste.string = @"";
            }
        }

 

  } // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. }

 

posted on 2016-06-14 10:27  助金  阅读(2560)  评论(0编辑  收藏  举报