在实际开发中,我们常常需要用到正则来对一些字符串进行一些操作。如,身份证验证、手机号验证、邮箱验证等。iOS从4.0开始支持正则表达式,并抽象出两个类来供开发者使用 NSRegularExpression、NSTextCheckingResult。
NSRegularExpression
// 根据所给的正则表达式创建一个NSRegularExpression类的实例,假如正则无效,那么将返回空和error
+ (nullable NSRegularExpression *)regularExpressionWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error;
- (nullable instancetype)initWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error NS_DESIGNATED_INITIALIZER;
// 一个只读的正则表达式
@property (readonly, copy) NSString *pattern;
// 正则表达式选项
@property (readonly) NSRegularExpressionOptions options;
// 匹配的个数
@property (readonly) NSUInteger numberOfCaptureGroups;
// 为正则语句每一个匹配类型添加一个'\'
// @"[0-9]{0,}D$" 输出:\[0-9]\{0,\}D\$
+ (NSString *)escapedPatternForString:(NSString *)string;
NSRegularExpressionOptions
typedef NS_OPTIONS(NSUInteger, NSRegularExpressionOptions) {
// 不区分大小写
NSRegularExpressionCaseInsensitive = 1 << 0,
// 忽略正则中的空格和‘#-’前缀
NSRegularExpressionAllowCommentsAndWhitespace = 1 << 1,
// 完整的正则,及不做其他处理
NSRegularExpressionIgnoreMetacharacters = 1 << 2,
// 允许‘.’去匹配任意一个字符 ,包括行分隔符'\n'
NSRegularExpressionDotMatchesLineSeparators = 1 << 3,
// 允许‘^’和‘$’匹配开始和结束
NSRegularExpressionAnchorsMatchLines = 1 << 4,
// 设置\n为唯一的行分隔符,否则所有的都有效。
NSRegularExpressionUseUnixLineSeparators = 1 << 5,
// 使用Unicode TR#29标准作为词的边界,否则所有传统正则表达式的词边界都有效
NSRegularExpressionUseUnicodeWordBoundaries = 1 << 6
};
NSMatching
typedef NS_OPTIONS(NSUInteger, NSMatchingOptions) {
//找到最长的匹配字符串后调用block回调
NSMatchingReportProgress = 1 << 0,
//找到任何一个匹配串后都回调一次block
NSMatchingReportCompletion = 1 << 1,
//从匹配范围的开始出进行极限匹配
NSMatchingAnchored = 1 << 2,
//允许匹配的范围超出设置的范围
NSMatchingWithTransparentBounds = 1 << 3,
//禁止^和$自动匹配行还是和结束
NSMatchingWithoutAnchoringBounds = 1 << 4
};
typedef NS_OPTIONS(NSUInteger, NSMatchingFlags) {
//匹配到最长串是被设置
NSMatchingProgress = 1 << 0,
//全部分配完成后被设置
NSMatchingCompleted = 1 << 1,
//匹配到设置范围的末尾时被设置
NSMatchingHitEnd = 1 << 2,
//当前匹配到的字符串在匹配范围的末尾时被设置
NSMatchingRequiredEnd = 1 << 3,
//由于错误导致的匹配失败时被设置
NSMatchingInternalError = 1 << 4
};
//这个方法会返回一个结果数组,将所有匹配的结果返回
- (NSArray<NSTextCheckingResult *> *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
//这个方法会返回匹配到得字符串的个数
- (NSUInteger)numberOfMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
//这个方法会返回第一个查询到得结果,这个NSTextCheckingResult对象中有一个range属性,可以得到匹配到的字符串的范围。
- (nullable NSTextCheckingResult *)firstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
//这个方法直接返回匹配到得范围,NSRange。
- (NSRange)rangeOfFirstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
NSReplacement
// 获取替换过的字符串
- (NSString *)stringByReplacingMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)templ;
// 获取需要替换的个数
- (NSUInteger)replaceMatchesInString:(NSMutableString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)templ;
例子
NSString *url = @"NSString *url = @"kkkhttp://15215990299www.baidu15530628270.com";";
NSString *regularString = @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[358]+\\d{9}|\\d{8}|\\d{7}";
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regularString options:0 error:nil];
if (regular) {
NSTextCheckingResult *result = [regular firstMatchInString:url options:NSMatchingReportProgress range:NSMakeRange(0, url.length)];
NSRange range = result.range;
NSLog(@"result 1: %@",[url substringWithRange:range]);
NSArray *arr = [regular matchesInString:url options:NSMatchingReportProgress range:NSMakeRange(0, url.length)];
for (NSTextCheckingResult *result in arr) {
NSLog(@"result >>: %@",[url substringWithRange:result.range]);
}
NSLog(@"%@",[regular stringByReplacingMatchesInString:url options:NSMatchingReportProgress range:NSMakeRange(0, url.length) withTemplate:@"J"]);
NSMutableString *str = [NSMutableString stringWithString:url];
NSLog(@"%ld, %@",[regular replaceMatchesInString:str options:0 range:NSMakeRange(0, url.length) withTemplate:@"J"], str);
}
// 结果
2017-05-11 16:49:15.017 CCZCheckResult[1245:139047] result 1: 15215990299
2017-05-11 16:49:15.018 CCZCheckResult[1245:139047] result >>: 15215990299
2017-05-11 16:49:15.018 CCZCheckResult[1245:139047] result >>: 15530628270
2017-05-11 16:49:15.018 CCZCheckResult[1245:139047] kkkhttp://Jwww.baiduJ.com
2017-05-11 16:49:15.018 CCZCheckResult[1245:139047] 2, kkkhttp://Jwww.baiduJ.com
网友评论