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】
  • 相关阅读:
    Python 用 matplotlib 中的 plot 画图
    python--serial串口通信
    verilog,vhdl,bdf文件一起综合
    项目小笔记2--qt designer 修改字体,部件拖入layout,引用time模块延时,正则表达式判断输入,进制转换,部件固定大小,graphics view显示图片,消息提示框使用
    虚拟环境下通过pyinstaller 打包
    FPGA--IIC通信
    FPGA--SPI通信
    verilog 语法
    【C_Language】---队列和栈的C程序实现
    【C_Language】---C文件学习
  • 原文地址:https://www.cnblogs.com/xiu619544553/p/5590100.html
Copyright © 2011-2022 走看看