RegexKitLite 使用详解

来源:互联网 发布:初中文化能学plc编程吗 编辑:程序博客网 时间:2024/05/16 06:16

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  
        若参数capture0L表示全部字符 --ishtml
        若参数capture1L表示正则表达式中第一个括号内匹配的字符(若不存在第一个括号,则argument is not valid程序崩溃)--is
        若参数capture2L表示正则表达式中第二个括号内匹配的字符(若不存在第二个括号,则argument is not valid程序崩溃)--ht
        若参数capture3L表示正则表达式中第三个括号内匹配的字符(若不存在第三个括号,则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

0 0
原创粉丝点击