您的位置:首页 > 移动开发 > IOS开发

RegexKitLite 使用详解

2017-02-14 17:37 281 查看
1.匹配字符,并通过参数删选匹配字符

- (NSString *)RKL_METHOD_PREPEND(stringByMatching):(NSString*)regex capture:(NSInteger)capture;
NSString* searchString1 = @"http://www.e//isxample.com:80222080/http//isindex.ishtml";

NSString* regexString1 = @"(is)(ht)(ml)";

NSLog(@"portInteger: '%@'", [searchString1stringByMatching:regexString1capture:3L]
);

/*

匹配字符 : ishtml

若参数capture:0L表示全部字符 --ishtml

若参数capture:1L表示正则表达式中第一个括号内匹配的字符(若不存在第一个括号,则argument is not valid程序崩溃)--is

若参数capture:2L表示正则表达式中第二个括号内匹配的字符(若不存在第二个括号,则argument is not valid程序崩溃)--ht

若参数capture:3L表示正则表达式中第三个括号内匹配的字符(若不存在第三个括号,则argument is not valid程序崩溃)
-- ml

*/

NSString *searchString = @"<p>\r<img src=\"http://192.168.0.180/mobilemobile/UpFile/20140804134426.jpg\" /></p>\r<p>\r<span id=\"LabHome\">The
information contained in this Manual cover all the services you need to participate successfully in the Exhibition. It provides you with all the necessary information on Exhibition Services and Order Forms to ensure a smooth <img src=\"http://192.168.0.180/mobilemobile/UpFile/14758.gif\"
/>and trouble-free run-up to MEDICAL MANUFACTURING ASIA 2014.</span></p>";

NSString *regexString = @"<img src=\"[^>]*\" />";

// 将匹配后的字符存放于数组当中

NSArray *matchArray = [searchString componentsMatchedByRegex:regexString];

NSLog(@"%@",matchArray);

// 将匹配后的字符替换成指定字符

NSString* matchString = [searchString stringByReplacingOccurrencesOfRegex:regexStringwithString:@"||================||"];

NSLog(@"%@",matchString);

NSString* searchString1 =@"http://www.e//isxample.com:80222080/http//isindex.ishtml";

NSString* regexString1 = @"(isi)(ndex)";

NSLog(@"%@",[searchString1 componentsMatchedByRegex:regexString1]);

NSLog(@"%@",[searchString1 captureComponentsMatchedByRegex:regexString1]);

参考博文:http://blog.csdn.NET/ios_long/article/details/6720650
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息