NSRegularExpression类
创建方法
+ (nullable NSRegularExpression *)regularExpressionWithPattern:(NSString *)pattern
options:(NSRegularExpressionOptions)options error:(NSError **)error;
- (nullable instancetype)initWithPattern:(NSString *)pattern
options:(NSRegularExpressionOptions)options error:(NSError **)error
NSRegularExpressionOptions
参数
常量 | 说明 |
---|---|
NSRegularExpressionCaseInsensitive | 不区分大小写 |
NSRegularExpressionAllowCommentsAndWhitespace | 忽略空格和# |
NSRegularExpressionIgnoreMetacharacters | 把正则表达式整体作为字符串处理 |
NSRegularExpressionDotMatchesLineSeparators | 点匹配任何字符,包括行分隔符 |
NSRegularExpressionAnchorsMatchLines | 允许^和$在匹配的开始和结束行 |
NSRegularExpressionUseUnixLineSeparators | 设置 \n 为唯一的行分隔符 |
NSRegularExpressionUseUnicodeWordBoundaries | 使用Unicode TR#29标准作为词的边界 |
主要方法
// 返回第一个匹配成功的结果
- (nullable NSTextCheckingResult *)firstMatchInString:(NSString *)string
options:(NSMatchingOptions)options range:(NSRange)range;
// 返回第一个匹配成功结果的范围
- (NSRange)rangeOfFirstMatchInString:(NSString *)string
options:(NSMatchingOptions)options range:(NSRange)range;
// 返回指定字符串范围内符合正则条件的字符串的个数
- (NSUInteger)numberOfMatchesInString:(NSString *)string
options:(NSMatchingOptions)options range:(NSRange)range;
// 返回字符串指定范围内的符合正则的字符串结果
- (NSArray<NSTextCheckingResult *> *)matchesInString:(NSString *)string
options:(NSMatchingOptions)options range:(NSRange)range;
// block方式遍历出合适的结果
- (void)enumerateMatchesInString:(NSString *)string
options:(NSMatchingOptions)options
range:(NSRange)range
usingBlock:(void (NS_NOESCAPE ^)(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL *stop))block;
替换字符串
// 用新字段替换原文本中的对应字段,并返回操作后的NSString
- (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 *pattern = @"a[B|C]d";
NSRegularExpressionOptions option = NSRegularExpressionCaseInsensitive;
NSError *error = nil;
NSString *str = @"abc abd acb acd adb adc";
// 0
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSLog(@"%ld", [regular numberOfMatchesInString:str options:0 range:NSMakeRange(0, str.length)]);
// 2
regular = [[NSRegularExpression alloc] initWithPattern:pattern options:option error:&error];
NSLog(@"%ld", [regular numberOfMatchesInString:str options:0 range:NSMakeRange(0, str.length)]);
// abd
NSTextCheckingResult *result = [regular firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (result.range.location != NSNotFound) {
NSLog(@"firstMatchInString result: %@", [str substringWithRange:result.range]);
}
// abd
NSRange range = [regular rangeOfFirstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
if (range.location != NSNotFound) {
NSLog(@"rangeOfFirstMatchInString result: %@", [str substringWithRange:range]);
}
// abd
// acd
NSArray *results = [regular matchesInString:str options:0 range:NSMakeRange(0, str.length)];
for (NSTextCheckingResult *result in results) {
NSLog(@"matchesInString result: %@", [str substringWithRange:result.range]);
}
// abd
// acd
[regular enumerateMatchesInString:str options:0 range:NSMakeRange(0, str.length)
usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSLog(@"enumerateMatchesInString result: %@", [str substringWithRange:result.range]);
}];
// abc ### acb ### adb adc
NSString *replaceStr = [regular stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, str.length) withTemplate:@"###"];
NSLog(@"stringByReplacingMatchesInString result: %@", replaceStr);
// 2
// abc ### acb ### adb adc
NSMutableString *mutableString = [NSMutableString stringWithString:str];
NSUInteger number = [regular replaceMatchesInString:mutableString options:0 range:NSMakeRange(0, mutableString.length) withTemplate:@"###"];
NSLog(@"replaceMatchesInString number = %ld result: %@", number, mutableString);
输出
0
2
firstMatchInString result: abd
rangeOfFirstMatchInString result: abd
matchesInString result: abd
matchesInString result: acd
enumerateMatchesInString result: abd
enumerateMatchesInString result: acd
stringByReplacingMatchesInString result: abc ### acb ### adb adc
replaceMatchesInString number = 2 result: abc ### acb ### adb adc