NSRegularExpression 使用

需求:

// 后台返回的某个实体
reminder = {
    cost = 1,
    type = 1,
    template = 可免费做某事{time}分钟,超过将按{cost}K元收费,
    time = 1
}
template 对应的字符串为后台返回,里面会动态出现 {key} 未知个数的key,然后根据 {key} 找到 reminder 实体里面相应的 key 对应的内容,替换 template ,拼成一段完成的话。


我的实现方案:
// 公共部分
NSMutableString *title = [[NSMutableString alloc] initWithString:@"可免费做某事{time}分钟,超过将按{cost}元收费"];
    NSDictionary *dict = @{@"time" : @"1",
                           @"cost" : @"1"};

方案一:

#pragma mark - 方案一
- (void)combineString:(NSString *)origalTitle dic:(NSDictionary *)dict {
    if (!origalTitle.length) { return; }
    NSMutableString *title = [[NSMutableString alloc] initWithString:origalTitle];
    NSString *pattern = @"\\{\\w*\\}";
    NSRegularExpression *regx = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSArray<NSTextCheckingResult *> *resultArr = [regx matchesInString:title options:0 range:NSMakeRange(0, title.length)];
    if (!resultArr.count) { return; }
    
    [resultArr enumerateObjectsUsingBlock:^(NSTextCheckingResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSRange range = obj.range;
        NSString *key = [title substringWithRange:range];
        NSString *newKey = [key substringWithRange:NSMakeRange(1, key.length - 2)];
        
        NSString *replace = [regx stringByReplacingMatchesInString:title options:0 range:range withTemplate:dict[newKey]];
        NSLog(@"replace____%@", replace);
        
        [self combineString:replace dic:dict];
        
        *stop = YES;
    }];
}

方案二:

#pragma mark - 方案二
- (void)sec_combineString:(NSString *)origalTitle dic:(NSDictionary *)dict {
    if (!origalTitle.length) { return; }
    NSMutableString *title = [[NSMutableString alloc] initWithString:origalTitle];
    NSString *pattern = @"\\{\\w*\\}";
    NSRegularExpression *regx = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    
    // 匹配结果
    __block NSMutableArray<NSString *> *rangeArray = [NSMutableArray array];
    [regx enumerateMatchesInString:title options:0 range:NSMakeRange(0, title.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
        if (flags != NSMatchingInternalError) {
            [rangeArray addObject:NSStringFromRange(result.range)];
        }
    }];
    
    // 遍历NSRange数组
    [rangeArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSRange range = NSRangeFromString(obj);
        NSString *key = [title substringWithRange:range];
        NSString *newKey = [key substringWithRange:NSMakeRange(1, key.length - 2)];
        
        NSString *replace = [regx stringByReplacingMatchesInString:title options:0 range:range withTemplate:dict[newKey]];
        [self sec_combineString:replace dic:dict];
        *stop = YES;
    }];
}
NSRegularExpression 入门介绍,可以参考我之前的一篇博客!传送门
关于正则表达式的入门级教程,可以参考该文章,我也是参考这边篇文章做的pattern。传送门

尊重作者劳动成果,转载请注明: 【kingdev】
posted @ 2016-06-16 10:27  Kingdev  阅读(1407)  评论(0编辑  收藏  举报