IOS txt小说断章正则表达式实现

1 篇文章 0 订阅
0 篇文章 0 订阅
- (NSArray*)extractChapterList
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString* regPattern = @"(\\s)+[第]{0,1}[0-9一二三四五六七八九十百千万]+章[ \t]*(\\S)*";
        NSError* error = nil;
        NSData* contentsData = [self readData:0 length:_fileSize];
        NSString* contentsStr = [[NSString alloc] initWithData:contentsData
                                                      encoding:_fileEncode];
        NSCharacterSet* whiteSpaceSet = [NSCharacterSet characterSetWithCharactersInString:@" \f\n\r\t\v"];
        NSRegularExpression* regExp = [NSRegularExpression regularExpressionWithPattern:regPattern
                                                                                options:NSRegularExpressionCaseInsensitive
                                                                                  error:&error];
        __block int64_t offsetInFile = 0;
        __weak typeof(self) _self = self;
        [regExp enumerateMatchesInString:contentsStr
                                 options:NSMatchingReportCompletion
                                   range:NSMakeRange(0, contentsStr.length)
                              usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            
                                  NSRange range = [result range];
                                  if (range.length > 0) {
        
                                      NSString* chapterTitle = [[contentsStr substringWithRange:range] stringByTrimmingCharactersInSet:whiteSpaceSet];
                                      NSUInteger startSearchPos = MAX(0, offsetInFile);
                                      offsetInFile = [_self chapterOffsetInFile:contentsData
                                                                searchPosition:startSearchPos
                                                                  chapterTitle:chapterTitle];
                                      dispatch_sync(dispatch_get_main_queue(), ^{
                                          [_self publishProgress:chapterTitle offsetInFile:offsetInFile];
                                      });
                                  }
                              }];

    });
    
    return nil;
}

// Run in main thread to update UI
- (void)publishProgress:(NSString*)chapterTitle offsetInFile:(uint64_t)offset
{
    NSLog(@"loc:%llu title:%@",offset, chapterTitle);
}

- (int64_t)chapterOffsetInFile:(NSData*)fileContents
                searchPosition:(NSUInteger)searchPosition
                  chapterTitle:(NSString*)chapterTitle
{
    NSData* titleData = [chapterTitle dataUsingEncoding:_fileEncode];
    NSRange chapterRange = [fileContents rangeOfData:titleData
                                             options:kNilOptions
                                               range:NSMakeRange(searchPosition, [fileContents length]-searchPosition)];
    if (chapterRange.length > 0) {
        NSLog(@"range : (%ld,%ld)", chapterRange.location, chapterRange.length);
        return chapterRange.location;
    }
    return -1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值