正则表达式快使用场景
2016-09-21 本文已影响244人
ShenYj
平时写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顶部弹出的搜索栏中,点击放大镜按钮

选择
Edit Find Options...
选项
在
Matching Style
后选择Regular Expression

3.选择好后,将刚刚复制的一条数据粘贴到输入框内

4.因为存在
[]
中括号的原因,还不能进行匹配,需要进行转义@"name":@"", @"backgroundColor": \[UIColor redColor\]
这样才能进行数据匹配(一行)

5.伪造数据中前面都一样,只有后面的颜色不同,想要实现全部的匹配,需要将不同处进行通配处理
将
RedColor
改为(.*?)
,这样就通配了全部的数据
6.填充前面缺失的内容,将
Find
改为Replacement

7.将搜索框内的内容复制一份到下面的输入框内,这里代表将要将匹配到的数据替换成的内容
将需要填充的地方加上
$1(代表对应上面匹配条件的第一个小括号,如果有两个括号,同理$2对应)
,除了前面需要将匹配到的颜色填充进去外,我们匹配到的数据原本颜色部分也需要改成$1
, @"name":@"$1", @"backgroundColor": \[UIColor $1\]

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的方式不一定比这种方式慢,但多掌握一样技能还是比较有用的.