//
// ViewController.h
// 正则表达式练习
//
// Created by zengchunjun on 16/10/8.
// Copyright © 2016年 zeng. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// 正则表达式练习
//
// Created by zengchunjun on 16/10/8.
// Copyright © 2016年 zeng. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)NSMutableArray *testArray;
@property (nonatomic,strong)NSMutableArray *phoneNumbers;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.phoneNumbers = [NSMutableArray array];
self.testArray = [[NSMutableArray alloc ] initWithObjects:@"137624 05923",@"136158fs78910",@"13062405923",@"13418 141808",@"134",@"1341352136138",@"1776 2405923",nil];
// 判断是否是手机号码
NSMutableArray *newArray = [self selectedPhoneNumber:self.testArray];
NSLog(@"%@",newArray);
// 替换指定的字符
[self replaceReguationItems:self.testArray];
NSLog(@"%@",self.testArray);
}
// 把是电话号码的元素替换成88888888
- (void)replaceReguationItems:(NSMutableArray *)aArray
{
/**
* 移动号段正则表达式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
// 一个判断是否是手机号码的正则表达式
NSString *pattern = [NSString stringWithFormat:@"(%@)|(%@)|(%@)",CM_NUM,CU_NUM,CT_NUM];
NSRegularExpression *regularExpression = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
for (int i = 0; i < aArray.count; i++) {
NSString *item = aArray[i];
item = [item stringByReplacingOccurrencesOfString:@" " withString:@""];
NSArray *matches = [regularExpression matchesInString:item options:0 range:NSMakeRange(0, item.length)];
if (matches.count == 1 && item.length == 11) {
for (NSTextCheckingResult *matchResult in matches) {
NSString *replacedStr = [regularExpression stringByReplacingMatchesInString:item options:0 range:matchResult.range withTemplate:@"88888888"];
[self.testArray replaceObjectAtIndex:i withObject:replacedStr];
}
}
}
}
// 筛选出是手机号码的元素
- (NSMutableArray *)selectedPhoneNumber:(NSMutableArray *)aArray
{
/**
* 移动号段正则表达式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
// 一个判断是否是手机号码的正则表达式
NSString *pattern = [NSString stringWithFormat:@"(%@)|(%@)|(%@)",CM_NUM,CU_NUM,CT_NUM];
NSRegularExpression *regularExpression = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
for (int i = 0;i < aArray.count;i++) {
NSString *mobile = [aArray[i] stringByReplacingOccurrencesOfString:@" " withString:@""];
if (mobile.length != 11) {
continue;
}
// 无符号整型数据接收匹配的数据的数目
NSUInteger numbersOfMatch = [regularExpression numberOfMatchesInString:mobile options:NSMatchingReportProgress range:NSMakeRange(0, mobile.length)];
if (numbersOfMatch>0) {
NSLog(@"%d-手机号码:%@",i,mobile);
[self.phoneNumbers addObject:mobile];
}
}
return self.phoneNumbers;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end