//
// 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 = ;
NSInteger __block findCount = ;
[regular enumerateMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(, searchText.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSRange matchRange = result.range;
if (matchRange.length > && findCount > ) {
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(, searchText.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
NSRange matchRange = result.range;
if (matchRange.length > ) {
printf("---------\n%s\n",[[self textRemoveHeaderFooterWhiteLine:[searchText substringWithRange:NSMakeRange(, 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 = ;
NSInteger endIndex = contentText.length - ;
NSString *subText = @"";
while ((subText = [contentText substringWithRange:NSMakeRange(startIndex, )]) && ([subText isEqualToString:@"\r"] || [subText isEqualToString:@"\n"])) {
startIndex++;
}
if (startIndex < endIndex) {
while ((subText = [contentText substringWithRange:NSMakeRange(endIndex, )]) && ([subText isEqualToString:@"\r"] || [subText isEqualToString:@"\n"])) {
endIndex--;
}
}
if (startIndex <= endIndex) {
return [contentText substringWithRange:NSMakeRange(startIndex, endIndex-startIndex+)];
}
return @"";
} -(NSString*)exprotText:(NSString*)text
{
NSInteger length = text.length;
NSInteger beginIndex = [self contentIndexFor:text startIndex:];
if (beginIndex < length) {
return [text substringFromIndex:beginIndex];
}
else {
return @"";
}
} -(NSInteger)contentIndexFor:(NSString*)text startIndex:(NSInteger)startIndex
{
NSInteger length = text.length;
if ((length - startIndex) > ) {
NSString *header = [text substringWithRange:NSMakeRange(startIndex, )];
if ([header isEqualToString:@"{\\"]) {
NSInteger endIndex = startIndex + ;
while (endIndex < length && ![[text substringWithRange:NSMakeRange(endIndex, )] isEqualToString:@"}"]) {
endIndex++;
}
return [self contentIndexFor:text startIndex:endIndex+];
}
else {
return startIndex;
}
}
else {
return startIndex;
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end