码迷,mamicode.com
首页 > 移动开发 > 详细

正则表达式过滤手机号

时间:2016-06-14 11:30:29      阅读:811      评论:0      收藏:0      [点我收藏+]

标签:

最近写了手机卫士,有个小需求是将系统任何地方复制得到的手机号码 粘贴到自己的手机卫士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. }

 

正则表达式过滤手机号

标签:

原文地址:http://www.cnblogs.com/zhujin/p/5583119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!