//
// ViewController.m
// 字幕解析
//
// Created by admin on 2018/8/30.
// Copyright © 2018年 admin. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self findMatch];
}
-(void)findMatch
{
/*
444
00:48:25,670 --> 00:48:26,920
你就是死在那儿的
*/
//当被检索的字符串太大时,用block控制查找
NSString *srtText = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"mp4video" ofType:@"srt"] encoding:NSUTF8StringEncoding error:nil];
NSString *searchText = [NSString stringWithFormat:@"\n%@",srtText];
NSString *regexStr = @"[\\n|\\r\\n|\\r\\t]{1}[0-9]{1,}[\\n|\\r\\n|\\r\\t]{1}";//@"[\\n|\\r\\n|\\r\\t]{1}[0-9]{2,}:[0-9]{2}:[0-9]{2},[0-9]{3} --> [0-9]{2,}:[0-9]{2}:[0-9]{2},[0-9]{3}" ;
NSError *error;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
if (error) return;
NSInteger __block startIndex = 0;
NSInteger __block findCount = 0;
[regular enumerateMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(0, searchText.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSRange matchRange = result.range;
if (matchRange.length > 0 && findCount > 0) {
NSString *noteText = [srtText substringWithRange:NSMakeRange(startIndex, matchRange.location - startIndex)];
printf("*****************\n");
printf("%s\n",[noteText UTF8String]);
startIndex = matchRange.location;
[self exportNote:noteText];
printf("*****************\n");
}
findCount ++;
}];
}
-(void)exportNote:(NSString*)searchText
{
NSString *regexStr = @"[\\n|\\r\\n|\\r\\t]{1}[0-9]{2,}:[0-9]{2}:[0-9]{2},[0-9]{3} --> [0-9]{2,}:[0-9]{2}:[0-9]{2},[0-9]{3}";
NSError *error;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
if (error) return;
[regular enumerateMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(0, searchText.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSRange matchRange = result.range;
if (matchRange.length > 0) {
printf("---------\n%s\n",[[self textRemoveHeaderFooterWhiteLine:[searchText substringWithRange:NSMakeRange(0, matchRange.location)]] UTF8String]);
printf("---------\n%s\n",[[self textRemoveHeaderFooterWhiteLine:[searchText substringWithRange:NSMakeRange(matchRange.location,matchRange.length)]] UTF8String]);
printf("---------\n%s\n---------\n",[[self exprotText:[self textRemoveHeaderFooterWhiteLine:[searchText substringFromIndex:matchRange.location+matchRange.length]]] UTF8String]);
*stop = YES;
}
}];
}
-(NSString*)textRemoveHeaderFooterWhiteLine:(NSString*)contentText
{
NSInteger startIndex = 0;
NSInteger endIndex = contentText.length - 1;
NSString *subText = @"";
while ((subText = [contentText substringWithRange:NSMakeRange(startIndex, 1)]) && ([subText isEqualToString:@"\r"] || [subText isEqualToString:@"\n"])) {
startIndex++;
}
if (startIndex < endIndex) {
while ((subText = [contentText substringWithRange:NSMakeRange(endIndex, 1)]) && ([subText isEqualToString:@"\r"] || [subText isEqualToString:@"\n"])) {
endIndex--;
}
}
if (startIndex <= endIndex) {
return [contentText substringWithRange:NSMakeRange(startIndex, endIndex-startIndex+1)];
}
return @"";
}
-(NSString*)exprotText:(NSString*)text
{
NSInteger length = text.length;
NSInteger beginIndex = [self contentIndexFor:text startIndex:0];
if (beginIndex < length) {
return [text substringFromIndex:beginIndex];
}
else {
return @"";
}
}
-(NSInteger)contentIndexFor:(NSString*)text startIndex:(NSInteger)startIndex
{
NSInteger length = text.length;
if ((length - startIndex) > 2) {
NSString *header = [text substringWithRange:NSMakeRange(startIndex, 2)];
if ([header isEqualToString:@"{\\"]) {
NSInteger endIndex = startIndex + 2;
while (endIndex < length && ![[text substringWithRange:NSMakeRange(endIndex, 1)] isEqualToString:@"}"]) {
endIndex++;
}
return [self contentIndexFor:text startIndex:endIndex+1];
}
else {
return startIndex;
}
}
else {
return startIndex;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
来源:oschina
链接:https://my.oschina.net/u/4416802/blog/3845155