正则表达式过滤手机号

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

 

转载于:https://www.cnblogs.com/zhujin/p/5583119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值