美文网首页
正则表达式快使用场景

正则表达式快使用场景

作者: ShenYj | 来源:发表于2016-09-21 14:40 被阅读244次

平时写Demo会需要用到一些假数据,往往会需要用到数组、字典等,这里分享一个使用技巧,通过正则表达式快速的制造数据

例如,我需要在一个列表中展示一组数据:

第一步:先快速的包装一个数组

NSArray *demoArr = @[
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         ];

数组中先写了一个元素 , 然后进行CV操作

第二步:将backgroundColor对应的值来进行下处理,接下来只需要补充name的值即可,一般情况下,按照CV的方式把backgroundColor的值粘过去就行了,这里演示另外一种方式来实现

NSArray *demoArr = @[
                         
                         @{@"name":@"", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor yellowColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor purpleColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor grayColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor blueColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor blackColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor orangeColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor greenColor]},
                         @{@"name":@"", @"backgroundColor": [UIColor cyanColor]},
                         
                         ];

第三步:使用正则表达式的方式,来补充前面缺少的部分 (关键部分)
1.复制一条数据 @"name":@"", @"backgroundColor": [UIColor redColor]
2.CMD+F快速查找,在Xcode顶部弹出的搜索栏中,点击放大镜按钮

搜索.png
选择Edit Find Options...选项
Edit Find Options.png
Matching Style后选择Regular Expression
Regular Expression.png
3.选择好后,将刚刚复制的一条数据粘贴到输入框内
输入框.png
4.因为存在[]中括号的原因,还不能进行匹配,需要进行转义
@"name":@"", @"backgroundColor": \[UIColor redColor\]
这样才能进行数据匹配(一行)
转移后匹配数据.png
5.伪造数据中前面都一样,只有后面的颜色不同,想要实现全部的匹配,需要将不同处进行通配处理
RedColor改为(.*?),这样就通配了全部的数据
通配.png
6.填充前面缺失的内容,将Find改为Replacement
Replacement.png
7.将搜索框内的内容复制一份到下面的输入框内,这里代表将要将匹配到的数据替换成的内容
将需要填充的地方加上$1(代表对应上面匹配条件的第一个小括号,如果有两个括号,同理$2对应),除了前面需要将匹配到的颜色填充进去外,我们匹配到的数据原本颜色部分也需要改成$1, @"name":@"$1", @"backgroundColor": \[UIColor $1\]
替换.png
8.最后点击Replace按钮,一组假数据就制造完成了
NSArray *demoArr = @[
                         
                         @{@"name":@"redColor", @"backgroundColor": [UIColor redColor]},
                         @{@"name":@"yellowColor", @"backgroundColor": [UIColor yellowColor]},
                         @{@"name":@"purpleColor", @"backgroundColor": [UIColor purpleColor]},
                         @{@"name":@"grayColor", @"backgroundColor": [UIColor grayColor]},
                         @{@"name":@"blueColor", @"backgroundColor": [UIColor blueColor]},
                         @{@"name":@"blackColor", @"backgroundColor": [UIColor blackColor]},
                         @{@"name":@"orangeColor", @"backgroundColor": [UIColor orangeColor]},
                         @{@"name":@"greenColor", @"backgroundColor": [UIColor greenColor]},
                         @{@"name":@"cyanColor", @"backgroundColor": [UIColor cyanColor]},
                         
                         ];

虽然CV的方式不一定比这种方式慢,但多掌握一样技能还是比较有用的.

相关文章

  • 正则表达式快使用场景

    平时写Demo会需要用到一些假数据,往往会需要用到数组、字典等,这里分享一个使用技巧,通过正则表达式快速的制造数据...

  • git log --grep 用法

    使用方法:git log 支持正则表达式搜索提交消息 git log --grep 使用场景:比...

  • 面试问题记录(三) Day52 2019-01-10

    string 具体实现,过滤,排序 正则表达式及使用效率 排序与去重 并发应用 各场景使用情况 静态块的使用与线程...

  • 我理解的正则表达式语法

    想弄明白一下问题: 该不该用正则表达式? 什么时候用?最贱使用场景? 怎么用? 通用正则表达式语法 正则...

  • 正则表达式

    正则表达式应用场景:1.在IDE中使用正则表达式处理规则复杂的字符串查找、替换需求2.在JavaScript程序设...

  • swift 自定义正则表达式运算符 =~

    swift 至今并没有在语言层面上支持正则表达式,可能在开发app时正则表达式使用的场景并不多。 封装 在 Coc...

  • 正则表达式(regex)

    正则表达式 Regular expression ,常常被简写为 regex .在各种匹配场景下有着十分广泛的使用...

  • 35、Excel 执行正则数据替换 ETRegexReplace

    函数名:ETRegexReplace 函数功能:执行正则表达式,完成数据的替换工作; 应用场景: 1、使用正...

  • 34、Excel 执行正则数据匹配 ETRegexMatch

    函数名:ETRegexMatch 函数功能:使用正则表达式,完成数据的匹配工作 应用场景: 1、如果会正则表...

  • 正则表达式使用场景

    jmeter正则表达式提取器 (AC.) ():提取一个字符串 AC. :第一个字符必须是A,第二个字符必须是C,...

网友评论

      本文标题:正则表达式快使用场景

      本文链接:https://www.haomeiwen.com/subject/wtjaettx.html