IOS中NSPredicate和NSRegularExpression校验正则表达式区别

时间:2022-06-11 14:36:11

在代码开发过程中,我们经常需要用来校验邮箱、手机号等等,这个时候就需要用到正则表达式。在iOS开发中,能用来做正则校验的有两个 NSPredicate 和 NSRegularExpression

NSPredicate

NSPredicate 能用来简单做正则校验,但是它的问题是存在校验不出来的情况。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//NSString+RegEx.h
#import <Foundation/Foundation.h>
@interface NSString (RegEx)
#pragma mark - NSPredicate
- (BOOL)mars_matchedByPrdicateToRegEx:(NSString *)regEx;
@end
//NSString+RegEx.m
#import "NSString+RegEx.h"
@implementation NSString (RegEx)
#pragma mark - NSPredicate
- (BOOL)mars_matchedByPrdicateToRegEx:(NSString *)regEx{
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];
  return [predicate evaluateWithObject:self];
}
 
@end

NSRegularExpression (推荐)

NSRegularExpression 相对于 NSPredicate 功能就强大的多了,这也是iOS正则校验的正统路子。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//NSString+RegEx.h
#import <Foundation/Foundation.h>
 
@interface NSString (RegEx)
 
#pragma mark - NSRegularExpression
 
//校验是否匹配
- (BOOL)mars_matchedToRegEx:(NSString *)regEx;
 
//匹配到的第一个字符串
- (NSString *)mars_firstMatchToRegEx:(NSString *)regEx;
 
//所有匹配的字符串
- (NSArray *)mars_matchesToRegEx:(NSString *)regEx;
 
//替换匹配到的字符串
- (NSString *)mars_stringByReplaceMatchesToRegEx:(NSString *)regEx replaceString:(NSString *)replaceString;
 
@end
 
//NSString+RegEx.m
#import "NSString+RegEx.h"
 
@implementation NSString (RegEx)
 
#pragma mark - NSRegualrExpression
 
//校验是否匹配
- (BOOL)mars_matchedToRegEx:(NSString *)regEx{
  
  NSError *error;
  NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error];
  
  NSUInteger number = [regularExpression numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)];
  return number != 0;
}
 
//匹配到的第一个字符串
- (NSString *)mars_firstMatchToRegEx:(NSString *)regEx{
  NSError *error;
  NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error];
  NSTextCheckingResult *firstMatch = [regularExpression firstMatchInString:self options:0 range:NSMakeRange(0, self.length)];
  if (firstMatch) {
    NSString *result = [self substringWithRange:firstMatch.range];
    return result;
  }
  return nil;
}
 
//所有匹配的字符串
- (NSArray *)mars_matchesToRegEx:(NSString *)regEx{
  NSError *error;
  NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error];
  
  NSArray *matchArray = [regularExpression matchesInString:self options:0 range:NSMakeRange(0, self.length)];
  
  NSMutableArray *array = [NSMutableArray array];
  if (matchArray.count != 0) {
    for (NSTextCheckingResult *match in matchArray) {
      NSString *result = [self substringWithRange:match.range];
      [array addObject:result];
    }
  }
  
  return array;
}
 
//替换匹配到的字符串
- (NSString *)mars_stringByReplaceMatchesToRegEx:(NSString *)regEx replaceString:(NSString *)replaceString{
  NSError *error;
  NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error];
 
  return [regularExpression stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, self.length) withTemplate:replaceString];
}
 
@end

最后我们看到,还是推荐大家使用NSRegularExpression来做正则的校验,如果大家在学习中有更好的解决方法或者心得,可以在下方的留言区讨论。