RegexKitLite学习
2016-01-29 17:19
155 查看
http://blog.sina.com.cn/s/blog_71715bf8010165t0.html
准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中。
然后还要加载framework libicucore.dylib ,因为RegexKitLite是调用这个里面的API,苹果规定过不能使用私有的api和没有发布的api。实际上RegexKitLite对NSString做了扩展,目前只支持NSString,对我来说也够了...
基本使用的例子(更多信息参看 官方文档 )
1.
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
NSError *error = NULL;
matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
NSLog(@"matchedRange: %@" , NSStringFromRange(matchedRange));
// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
NSString *matchedString = [searchString substringWithRange:matchedRange];
NSLog(@"matchedString: '%@'" , matchedString);
// 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
NSString *searchString = @"This is neat.";
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
NSError *error = NULL;
matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));
// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
NSString *matchedString = [searchString substringWithRange:matchedRange];
NSLog(@"matchedString: '%@'", matchedString);
// 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
2.找到第一个匹配并返回一个NSString
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
NSLog(@"matchedString: '%@'" , matchedString);
// 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
NSString *searchString = @"This is neat.";
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
NSLog(@"matchedString: '%@'", matchedString);
// 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
3.查找和替换,加括号和概念和Python中的一样,$1指代第一个括号中的内容
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"//b(//w+)//b" ;
NSString *replaceWithString = @"{$1}" ;
NSString *replacedString = NULL;
replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
//NSMutableString可以直接替换,并返回替换的次数
NSLog(@"replaced string: '%@'" , replacedString);
// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat." ];
NSString *regexString = @"//b(//w+)//b" ;
NSString *replaceWithString = @"{$1}" ;
NSUInteger replacedCount = 0UL;
replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
NSLog(@"count: %lu string: '%@'" , (u_long)replacedCount, mutableString);
// 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
NSString *searchString = @"This is neat.";
NSString *regexString = @"//b(//w+)//b";
NSString *replaceWithString = @"{$1}";
NSString *replacedString = NULL;
replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
//NSMutableString可以直接替换,并返回替换的次数
NSLog(@"replaced string: '%@'", replacedString);
// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat."];
NSString *regexString = @"//b(//w+)//b";
NSString *replaceWithString = @"{$1}";
NSUInteger replacedCount = 0UL;
replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);
// 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
4.用于拆分,返回一个拆分后的字符串数组
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"//s+" ;
NSArray *splitArray = NULL;
splitArray = [searchString componentsSeparatedByRegex:regexString];
// splitArray == { @"This", @"is", @"neat." }
NSLog(@"splitArray: %@" , splitArray);
NSString *searchString = @"This is neat.";
NSString *regexString = @"//s+";
NSArray *splitArray = NULL;
splitArray = [searchString componentsSeparatedByRegex:regexString];
// splitArray == { @"This", @"is", @"neat." }
NSLog(@"splitArray: %@", splitArray);
5.返回所有匹配的字符串数组,这个例子中虽然有多个括号,但是 componentsMatchedByRegex不管
NSString *searchString = @ "$10.23, $1024.42, $3099" ;
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
NSArray *matchArray = NULL;
matchArray = [searchString componentsMatchedByRegex:regexString];
// matchArray == { @"$10.23", @"$1024.42", @"$3099" };
NSLog(@"matchArray: %@" , matchArray);
6.返回所有匹配的字符串数组处理所有的括号
NSString *searchString = @"$10.23, $1024.42, $3099" ;
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
NSArray *capturesArray = NULL;
capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
NSLog(@"capturesArray: %@" , capturesArray);
输出结果:
shell% ./capturesArray↵
2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
(
"$10.23" ,
"10.23" ,
10,
23
),
(
"$1024.42" ,
"1024.42" ,
1024,
42
),
(
"$3099" ,
3099,
3099,
""
)
)
NSString *searchString = @"$10.23, $1024.42, $3099";
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
NSArray *matchArray = NULL;
matchArray = [searchString componentsMatchedByRegex:regexString];
// matchArray == { @"$10.23", @"$1024.42", @"$3099" };
NSLog(@"matchArray: %@", matchArray);
6.返回所有匹配的字符串数组处理所有的括号
NSString *searchString = @"$10.23, $1024.42, $3099";
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
NSArray *capturesArray = NULL;
capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
NSLog(@"capturesArray: %@", capturesArray);
输出结果:
shell% ./capturesArray↵
2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
(
"$10.23",
"10.23",
10,
23
),
(
"$1024.42",
"1024.42",
1024,
42
),
(
"$3099",
3099,
3099,
""
)
)
准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中。
然后还要加载framework libicucore.dylib ,因为RegexKitLite是调用这个里面的API,苹果规定过不能使用私有的api和没有发布的api。实际上RegexKitLite对NSString做了扩展,目前只支持NSString,对我来说也够了...
基本使用的例子(更多信息参看 官方文档 )
1.
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
NSError *error = NULL;
matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
NSLog(@"matchedRange: %@" , NSStringFromRange(matchedRange));
// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
NSString *matchedString = [searchString substringWithRange:matchedRange];
NSLog(@"matchedString: '%@'" , matchedString);
// 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
NSString *searchString = @"This is neat.";
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
NSRange matchedRange = NSMakeRange(NSNotFound, 0UL);
NSError *error = NULL;
matchedRange = [searchString rangeOfRegex:regexString options:RKLNoOptions inRange:searchRange capture:2L error:&error];
NSLog(@"matchedRange: %@", NSStringFromRange(matchedRange));
// 2008-03-18 03:51:16.530 test[51583:813] matchedRange: {5, 2},//匹配到‘is‘
NSString *matchedString = [searchString substringWithRange:matchedRange];
NSLog(@"matchedString: '%@'", matchedString);
// 2008-03-18 03:51:16.532 test[51583:813] matchedString: 'is' //生成子字符串
2.找到第一个匹配并返回一个NSString
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)" ;
NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
NSLog(@"matchedString: '%@'" , matchedString);
// 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
NSString *searchString = @"This is neat.";
NSString *regexString = @"(//w+)//s+(//w+)//s+(//w+)";
NSString *matchedString = [searchString stringByMatching:regexString capture:2L];
NSLog(@"matchedString: '%@'", matchedString);
// 2008-03-18 03:53:42.949 test[51583:813] matchedString: 'is'
3.查找和替换,加括号和概念和Python中的一样,$1指代第一个括号中的内容
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"//b(//w+)//b" ;
NSString *replaceWithString = @"{$1}" ;
NSString *replacedString = NULL;
replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
//NSMutableString可以直接替换,并返回替换的次数
NSLog(@"replaced string: '%@'" , replacedString);
// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat." ];
NSString *regexString = @"//b(//w+)//b" ;
NSString *replaceWithString = @"{$1}" ;
NSUInteger replacedCount = 0UL;
replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
NSLog(@"count: %lu string: '%@'" , (u_long)replacedCount, mutableString);
// 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
NSString *searchString = @"This is neat.";
NSString *regexString = @"//b(//w+)//b";
NSString *replaceWithString = @"{$1}";
NSString *replacedString = NULL;
replacedString = [searchString stringByReplacingOccurrencesOfRegex:regexString withString:replaceWithString];
//NSMutableString可以直接替换,并返回替换的次数
NSLog(@"replaced string: '%@'", replacedString);
// 2008-07-01 19:03:03.195 test[68775:813] replaced string: '{This} {is} {neat}.'
NSMutableString *mutableString = [NSMutableString stringWithString:@"This is neat."];
NSString *regexString = @"//b(//w+)//b";
NSString *replaceWithString = @"{$1}";
NSUInteger replacedCount = 0UL;
replacedCount = [mutableString replaceOccurrencesOfRegex:regexString withString:replaceWithString];
NSLog(@"count: %lu string: '%@'", (u_long)replacedCount, mutableString);
// 2008-07-01 21:25:43.433 test[69689:813] count: 3 string: '{This} {is} {neat}.'
4.用于拆分,返回一个拆分后的字符串数组
NSString *searchString = @ "This is neat." ;
NSString *regexString = @"//s+" ;
NSArray *splitArray = NULL;
splitArray = [searchString componentsSeparatedByRegex:regexString];
// splitArray == { @"This", @"is", @"neat." }
NSLog(@"splitArray: %@" , splitArray);
NSString *searchString = @"This is neat.";
NSString *regexString = @"//s+";
NSArray *splitArray = NULL;
splitArray = [searchString componentsSeparatedByRegex:regexString];
// splitArray == { @"This", @"is", @"neat." }
NSLog(@"splitArray: %@", splitArray);
5.返回所有匹配的字符串数组,这个例子中虽然有多个括号,但是 componentsMatchedByRegex不管
NSString *searchString = @ "$10.23, $1024.42, $3099" ;
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
NSArray *matchArray = NULL;
matchArray = [searchString componentsMatchedByRegex:regexString];
// matchArray == { @"$10.23", @"$1024.42", @"$3099" };
NSLog(@"matchArray: %@" , matchArray);
6.返回所有匹配的字符串数组处理所有的括号
NSString *searchString = @"$10.23, $1024.42, $3099" ;
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))" ;
NSArray *capturesArray = NULL;
capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
NSLog(@"capturesArray: %@" , capturesArray);
输出结果:
shell% ./capturesArray↵
2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
(
"$10.23" ,
"10.23" ,
10,
23
),
(
"$1024.42" ,
"1024.42" ,
1024,
42
),
(
"$3099" ,
3099,
3099,
""
)
)
NSString *searchString = @"$10.23, $1024.42, $3099";
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
NSArray *matchArray = NULL;
matchArray = [searchString componentsMatchedByRegex:regexString];
// matchArray == { @"$10.23", @"$1024.42", @"$3099" };
NSLog(@"matchArray: %@", matchArray);
6.返回所有匹配的字符串数组处理所有的括号
NSString *searchString = @"$10.23, $1024.42, $3099";
NSString *regexString = @"//$((//d+)(?://.(//d+)|//.?))";
NSArray *capturesArray = NULL;
capturesArray = [searchString arrayOfCaptureComponentsMatchedByRegex:regexString];
NSLog(@"capturesArray: %@", capturesArray);
输出结果:
shell% ./capturesArray↵
2009-05-06 03:25:46.852 capturesArray[69981:10b] capturesArray: (
(
"$10.23",
"10.23",
10,
23
),
(
"$1024.42",
"1024.42",
1024,
42
),
(
"$3099",
3099,
3099,
""
)
)
相关文章推荐
- 使用SLF4J打印日志---打印异常日志注意点
- 关于Android app首次安装完成后在安装界面直接“打开”应用再按home键返回桌面,重新进入app重复实例化launcher activity的问题的解决
- 看图识“字”(3)-评估数据水平
- Android Studio关于SVN的相关配置简介
- GCD,线程使用的一些笔记
- Java — 对本地文件的操作
- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
- 删除或清空具有外键约束的表数据报-ERROR 1701 (42000)
- 使用Visual Studio(VS)开发Qt程序代码提示功能的实现
- Excel02-快速无误输入多个零
- linux PATH环境变量设置与读取
- Android 开启百度客户端的导航系统
- http服务器下载文件
- Resource temporarily unavailable
- MySQL 多实例给root用户创建密码
- 160129关于xcode 7中object c与swift相互引用的问题。
- 看图识“字”(2)-数据校验
- Oracle 多个查询语句结果合并
- IOS只改变父视图的的透明度,不会改变子View的透明度
- MySQL 多实例启动和关闭脚本