zoukankan      html  css  js  c++  java
  • 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】
  • 相关阅读:
    移动端rem适配
    extern 关键字
    腾讯2014校园招聘软件开发类笔试试题
    堆,栈,堆栈
    转:对TCP/IP网络协议的深入浅出归纳
    转:程序员面试笔试宝典学习记录(一)
    求素数
    [C++]访问控制与继承(public,protect,private) 有时间再整理!!!
    面向对象的static关键字(类中的static关键字)
    腾讯校园招聘会笔试题2011.10.15
  • 原文地址:https://www.cnblogs.com/xiu619544553/p/5590100.html
Copyright © 2011-2022 走看看